forked from praeclarum/sqlite-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrencyTest.cs
193 lines (160 loc) · 5.8 KB
/
ConcurrencyTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
#if NETFX_CORE
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
using TearDown = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestCleanupAttribute;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#else
using NUnit.Framework;
#endif
namespace SQLite.Tests
{
[TestFixture]
public class ConcurrencyTest
{
public class TestObj
{
[AutoIncrement, PrimaryKey]
public int Id { get; set; }
public override string ToString()
{
return string.Format("[TestObj: Id={0}]", Id);
}
}
public class DbReader
{
private CancellationToken cancellationToken;
public DbReader(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
}
public Task Run()
{
var t = Task.Run(() =>
{
try
{
while (true)
{
//
// NOTE: Change this to readwrite and then it does work ???
// No more IOERROR
//
var flags = SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadOnly;
#if __IOS__
flags = SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite;
#endif
using (var dbConnection = new DbConnection(flags))
{
var records = dbConnection.Table<TestObj>().ToList();
System.Diagnostics.Debug.WriteLine($"{Environment.CurrentManagedThreadId} Read records: {records.Count}");
}
// No await so we stay on the same thread
Task.Delay(10).GetAwaiter().GetResult();
cancellationToken.ThrowIfCancellationRequested();
}
}
catch (OperationCanceledException)
{
}
});
return t;
}
}
public class DbWriter
{
private CancellationToken cancellationToken;
public DbWriter(CancellationToken cancellationToken)
{
this.cancellationToken = cancellationToken;
}
public Task Run()
{
var t = Task.Run(() =>
{
try
{
while (true)
{
using (var dbConnection = new DbConnection(SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite))
{
System.Diagnostics.Debug.WriteLine($"{Environment.CurrentManagedThreadId} Start insert");
for (var i = 0; i < 50; i++)
{
var newRecord = new TestObj()
{
};
dbConnection.Insert(newRecord);
}
System.Diagnostics.Debug.WriteLine($"{Environment.CurrentManagedThreadId} Inserted records");
}
// No await so we stay on the same thread
Task.Delay(1).GetAwaiter().GetResult();
cancellationToken.ThrowIfCancellationRequested();
}
}
catch (OperationCanceledException)
{
}
});
return t;
}
}
public class DbConnection : SQLiteConnection
{
private static string DbPath = GetTempFileName();
private static string GetTempFileName()
{
#if NETFX_CORE
var name = Guid.NewGuid() + ".sqlite";
return Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, name);
#else
return Path.GetTempFileName();
#endif
}
public DbConnection(SQLiteOpenFlags openflags) : base(DbPath, openflags)
{
this.BusyTimeout = TimeSpan.FromSeconds(5);
}
public void CreateTables()
{
CreateTable<TestObj>();
}
}
[SetUp]
public void Setup()
{
using (var dbConenction = new DbConnection(SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create))
{
dbConenction.CreateTables();
}
}
[Test]
public void TestLoad()
{
try
{
//var result = SQLitePCL.raw.sqlite3_threadsafe();
//Assert.AreEqual(2, result);
// Yes it's threadsafe on iOS
var tokenSource = new CancellationTokenSource();
var tasks = new List<Task>();
tasks.Add(new DbReader(tokenSource.Token).Run());
tasks.Add(new DbWriter(tokenSource.Token).Run());
// Wait 5sec
tokenSource.CancelAfter(5000);
Task.WhenAll(tasks).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Assert.Fail(ex.ToString());
}
}
}
}