forked from microsoft/FASTER
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionPerSessionTests.cs
More file actions
172 lines (139 loc) · 5.37 KB
/
FunctionPerSessionTests.cs
File metadata and controls
172 lines (139 loc) · 5.37 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FASTER.core;
using NUnit.Framework;
namespace FASTER.test
{
public struct RefCountedValue
{
public int ReferenceCount;
public long Value;
}
public class RefCountedAdder : FunctionsBase<int, RefCountedValue, long, Empty, Empty>
{
public int InitialCount;
public int InPlaceCount;
public int CopyCount;
public override void InitialUpdater(ref int key, ref long input, ref RefCountedValue value)
{
Interlocked.Increment(ref InitialCount);
value.Value = input;
value.ReferenceCount = 1;
}
public override bool InPlaceUpdater(ref int key, ref long input, ref RefCountedValue value)
{
Interlocked.Increment(ref InPlaceCount);
value.Value = input;
value.ReferenceCount++;
return true;
}
public override void CopyUpdater(ref int key, ref long input, ref RefCountedValue oldValue, ref RefCountedValue newValue)
{
Interlocked.Increment(ref CopyCount);
newValue.Value = input;
newValue.ReferenceCount = oldValue.ReferenceCount + 1;
}
}
public class RefCountedRemover : FunctionsBase<int, RefCountedValue, Empty, Empty, Empty>
{
public int InitialCount;
public int InPlaceCount;
public int CopyCount;
public override void InitialUpdater(ref int key, ref Empty input, ref RefCountedValue value)
{
Interlocked.Increment(ref InitialCount);
value.Value = 0;
value.ReferenceCount = 0;
}
public override bool InPlaceUpdater(ref int key, ref Empty input, ref RefCountedValue value)
{
Interlocked.Increment(ref InPlaceCount);
if (value.ReferenceCount > 0)
value.ReferenceCount--;
return true;
}
public override void CopyUpdater(ref int key, ref Empty input, ref RefCountedValue oldValue, ref RefCountedValue newValue)
{
Interlocked.Increment(ref CopyCount);
newValue.ReferenceCount = oldValue.ReferenceCount;
if (newValue.ReferenceCount > 0)
newValue.ReferenceCount--;
newValue.Value = oldValue.Value;
}
}
public class RefCountedReader : FunctionsBase<int, RefCountedValue, Empty, RefCountedValue, Empty>
{
public override void SingleReader(ref int key, ref Empty input, ref RefCountedValue value, ref RefCountedValue dst)
{
dst = value;
}
public override void ConcurrentReader(ref int key, ref Empty input, ref RefCountedValue value, ref RefCountedValue dst)
{
dst = value;
}
}
[TestFixture]
public class FunctionPerSessionTests
{
private IDevice _log;
private FasterKV<int, RefCountedValue> _faster;
private RefCountedAdder _adder;
private RefCountedRemover _remover;
private RefCountedReader _reader;
[SetUp]
public void Setup()
{
_log = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "\\FunctionPerSessionTests1.log", deleteOnClose: true);
_faster = new FasterKV<int, RefCountedValue>(128, new LogSettings()
{
LogDevice = _log,
});
_adder = new RefCountedAdder();
_remover = new RefCountedRemover();
_reader = new RefCountedReader();
}
[TearDown]
public void TearDown()
{
_faster.Dispose();
_faster = null;
_log.Close();
}
[Test]
public async Task Should_create_multiple_sessions_with_different_callbacks()
{
using (var adderSession = _faster.NewSession(_adder))
using (var removerSession = _faster.NewSession(_remover))
using (var readerSession = _faster.NewSession(_reader))
{
var key = 101;
var input = 1000L;
await adderSession.RMWAsync(ref key, ref input);
await adderSession.RMWAsync(ref key, ref input);
await adderSession.RMWAsync(ref key, ref input);
Assert.AreEqual(1, _adder.InitialCount);
Assert.AreEqual(2, _adder.InPlaceCount);
var empty = default(Empty);
await removerSession.RMWAsync(ref key, ref empty);
Assert.AreEqual(1, _remover.InPlaceCount);
var read = await readerSession.ReadAsync(ref key, ref empty);
var result = read.CompleteRead();
var actual = result.Item2;
Assert.AreEqual(2, actual.ReferenceCount);
Assert.AreEqual(1000L, actual.Value);
_faster.Log.FlushAndEvict(true);
await removerSession.RMWAsync(ref key, ref empty);
read = await readerSession.ReadAsync(ref key, ref empty);
result = read.CompleteRead();
actual = result.Item2;
Assert.AreEqual(1, actual.ReferenceCount);
Assert.AreEqual(1000L, actual.Value);
Assert.AreEqual(1, _remover.CopyCount);
}
}
}
}