|
1 | | -using System; |
2 | | -using System.Threading; |
3 | | -using System.Threading.Tasks; |
4 | | -using BitFaster.Caching.Atomic; |
5 | | -using FluentAssertions; |
6 | | -using Xunit; |
7 | | - |
8 | | -namespace BitFaster.Caching.UnitTests.Atomic |
9 | | -{ |
10 | | - public class AsyncAtomicFactoryTests |
11 | | - { |
12 | | - [Fact] |
13 | | - public void DefaultCtorValueIsNotCreated() |
14 | | - { |
15 | | - var a = new AsyncAtomicFactory<int, int>(); |
16 | | - |
17 | | - a.IsValueCreated.Should().BeFalse(); |
18 | | - a.ValueIfCreated.Should().Be(0); |
19 | | - } |
20 | | - |
21 | | - [Fact] |
22 | | - public void WhenValuePassedToCtorValueIsStored() |
23 | | - { |
24 | | - var a = new AsyncAtomicFactory<int, int>(1); |
25 | | - |
26 | | - a.ValueIfCreated.Should().Be(1); |
27 | | - a.IsValueCreated.Should().BeTrue(); |
28 | | - } |
29 | | - |
30 | | - [Fact] |
31 | | - public async Task WhenValueCreatedValueReturned() |
32 | | - { |
33 | | - var a = new AsyncAtomicFactory<int, int>(); |
34 | | - (await a.GetValueAsync(1, k => Task.FromResult(2))).Should().Be(2); |
35 | | - |
36 | | - a.ValueIfCreated.Should().Be(2); |
37 | | - a.IsValueCreated.Should().BeTrue(); |
38 | | - } |
39 | | - |
40 | | - [Fact] |
41 | | - public async Task WhenValueCreatedWithArgValueReturned() |
42 | | - { |
43 | | - var a = new AsyncAtomicFactory<int, int>(); |
44 | | - (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7)).Should().Be(8); |
45 | | - |
46 | | - a.ValueIfCreated.Should().Be(8); |
47 | | - a.IsValueCreated.Should().BeTrue(); |
48 | | - } |
49 | | - |
50 | | - [Fact] |
51 | | - public async Task WhenValueCreatedGetValueReturnsOriginalValue() |
52 | | - { |
53 | | - var a = new AsyncAtomicFactory<int, int>(); |
54 | | - await a.GetValueAsync(1, k => Task.FromResult(2)); |
55 | | - (await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(2); |
56 | | - } |
57 | | - |
58 | | - [Fact] |
59 | | - public async Task WhenValueCreatedArgGetValueReturnsOriginalValue() |
60 | | - { |
61 | | - var a = new AsyncAtomicFactory<int, int>(); |
62 | | - await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7); |
63 | | - (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 9)).Should().Be(8); |
64 | | - } |
65 | | - |
66 | | - [Fact] |
67 | | - public async Task WhenValueCreateThrowsValueIsNotStored() |
68 | | - { |
69 | | - var a = new AsyncAtomicFactory<int, int>(); |
70 | | - |
71 | | - Func<Task> getOrAdd = async () => { await a.GetValueAsync(1, k => throw new ArithmeticException()); }; |
72 | | - |
73 | | - await getOrAdd.Should().ThrowAsync<ArithmeticException>(); |
74 | | - |
75 | | - (await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(3); |
76 | | - } |
77 | | - |
78 | | - [Fact] |
79 | | - public async Task WhenCallersRunConcurrentlyResultIsFromWinner() |
80 | | - { |
81 | | - var enter = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
82 | | - var resume = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
83 | | - |
84 | | - var atomicFactory = new AsyncAtomicFactory<int, int>(); |
85 | | - var result = 0; |
86 | | - var winnerCount = 0; |
87 | | - |
88 | | - var first = atomicFactory.GetValueAsync(1, async k => |
89 | | - { |
90 | | - enter.SetResult(true); |
91 | | - await resume.Task; |
92 | | - |
93 | | - result = 1; |
94 | | - Interlocked.Increment(ref winnerCount); |
95 | | - return 1; |
96 | | - }); |
97 | | - |
98 | | - var second = atomicFactory.GetValueAsync(1, async k => |
99 | | - { |
100 | | - enter.SetResult(true); |
101 | | - await resume.Task; |
102 | | - |
103 | | - result = 2; |
104 | | - Interlocked.Increment(ref winnerCount); |
105 | | - return 2; |
106 | | - }); |
107 | | - |
108 | | - await enter.Task; |
109 | | - resume.SetResult(true); |
110 | | - |
111 | | - (await first).Should().Be(result); |
112 | | - (await second).Should().Be(result); |
113 | | - |
114 | | - winnerCount.Should().Be(1); |
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using BitFaster.Caching.Atomic; |
| 5 | +using FluentAssertions; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace BitFaster.Caching.UnitTests.Atomic |
| 9 | +{ |
| 10 | + public class AsyncAtomicFactoryTests |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void DefaultCtorValueIsNotCreated() |
| 14 | + { |
| 15 | + var a = new AsyncAtomicFactory<int, int>(); |
| 16 | + |
| 17 | + a.IsValueCreated.Should().BeFalse(); |
| 18 | + a.ValueIfCreated.Should().Be(0); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void WhenValuePassedToCtorValueIsStored() |
| 23 | + { |
| 24 | + var a = new AsyncAtomicFactory<int, int>(1); |
| 25 | + |
| 26 | + a.ValueIfCreated.Should().Be(1); |
| 27 | + a.IsValueCreated.Should().BeTrue(); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public async Task WhenValueCreatedValueReturned() |
| 32 | + { |
| 33 | + var a = new AsyncAtomicFactory<int, int>(); |
| 34 | + (await a.GetValueAsync(1, k => Task.FromResult(2))).Should().Be(2); |
| 35 | + |
| 36 | + a.ValueIfCreated.Should().Be(2); |
| 37 | + a.IsValueCreated.Should().BeTrue(); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task WhenValueCreatedWithArgValueReturned() |
| 42 | + { |
| 43 | + var a = new AsyncAtomicFactory<int, int>(); |
| 44 | + (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7)).Should().Be(8); |
| 45 | + |
| 46 | + a.ValueIfCreated.Should().Be(8); |
| 47 | + a.IsValueCreated.Should().BeTrue(); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public async Task WhenValueCreatedGetValueReturnsOriginalValue() |
| 52 | + { |
| 53 | + var a = new AsyncAtomicFactory<int, int>(); |
| 54 | + await a.GetValueAsync(1, k => Task.FromResult(2)); |
| 55 | + (await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(2); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task WhenValueCreatedArgGetValueReturnsOriginalValue() |
| 60 | + { |
| 61 | + var a = new AsyncAtomicFactory<int, int>(); |
| 62 | + await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 7); |
| 63 | + (await a.GetValueAsync(1, (k, a) => Task.FromResult(k + a), 9)).Should().Be(8); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public async Task WhenValueCreateThrowsValueIsNotStored() |
| 68 | + { |
| 69 | + var a = new AsyncAtomicFactory<int, int>(); |
| 70 | + |
| 71 | + Func<Task> getOrAdd = async () => { await a.GetValueAsync(1, k => throw new ArithmeticException()); }; |
| 72 | + |
| 73 | + await getOrAdd.Should().ThrowAsync<ArithmeticException>(); |
| 74 | + |
| 75 | + (await a.GetValueAsync(1, k => Task.FromResult(3))).Should().Be(3); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async Task WhenCallersRunConcurrentlyResultIsFromWinner() |
| 80 | + { |
| 81 | + var enter = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 82 | + var resume = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 83 | + |
| 84 | + var atomicFactory = new AsyncAtomicFactory<int, int>(); |
| 85 | + var result = 0; |
| 86 | + var winnerCount = 0; |
| 87 | + |
| 88 | + var first = atomicFactory.GetValueAsync(1, async k => |
| 89 | + { |
| 90 | + enter.SetResult(true); |
| 91 | + await resume.Task; |
| 92 | + |
| 93 | + result = 1; |
| 94 | + Interlocked.Increment(ref winnerCount); |
| 95 | + return 1; |
| 96 | + }); |
| 97 | + |
| 98 | + var second = atomicFactory.GetValueAsync(1, async k => |
| 99 | + { |
| 100 | + enter.SetResult(true); |
| 101 | + await resume.Task; |
| 102 | + |
| 103 | + result = 2; |
| 104 | + Interlocked.Increment(ref winnerCount); |
| 105 | + return 2; |
| 106 | + }); |
| 107 | + |
| 108 | + await enter.Task; |
| 109 | + resume.SetResult(true); |
| 110 | + |
| 111 | + (await first).Should().Be(result); |
| 112 | + (await second).Should().Be(result); |
| 113 | + |
| 114 | + winnerCount.Should().Be(1); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void WhenValueNotCreatedHashCodeIsZero() |
| 119 | + { |
| 120 | + new AsyncAtomicFactory<int, int>() |
| 121 | + .GetHashCode() |
| 122 | + .Should().Be(0); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void WhenValueCreatedHashCodeIsValueHashCode() |
| 127 | + { |
| 128 | + new AsyncAtomicFactory<int, int>(1) |
| 129 | + .GetHashCode() |
| 130 | + .Should().Be(1); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void WhenValueNotCreatedEqualsFalse() |
| 135 | + { |
| 136 | + var a = new AsyncAtomicFactory<int, int>(); |
| 137 | + var b = new AsyncAtomicFactory<int, int>(); |
| 138 | + |
| 139 | + a.Equals(b).Should().BeFalse(); |
115 | 140 | } |
116 | 141 |
|
117 | | - [Fact] |
118 | | - public void WhenValueNotCreatedHashCodeIsZero() |
119 | | - { |
120 | | - new AsyncAtomicFactory<int, int>() |
121 | | - .GetHashCode() |
122 | | - .Should().Be(0); |
123 | | - } |
124 | | - |
125 | | - [Fact] |
126 | | - public void WhenValueCreatedHashCodeIsValueHashCode() |
127 | | - { |
128 | | - new AsyncAtomicFactory<int, int>(1) |
129 | | - .GetHashCode() |
130 | | - .Should().Be(1); |
131 | | - } |
132 | | - |
133 | | - [Fact] |
134 | | - public void WhenValueNotCreatedEqualsFalse() |
135 | | - { |
136 | | - var a = new AsyncAtomicFactory<int, int>(); |
137 | | - var b = new AsyncAtomicFactory<int, int>(); |
138 | | - |
139 | | - a.Equals(b).Should().BeFalse(); |
140 | | - } |
141 | | - |
142 | | - [Fact] |
143 | | - public void WhenOtherValueNotCreatedEqualsFalse() |
144 | | - { |
145 | | - var a = new AsyncAtomicFactory<int, int>(1); |
146 | | - var b = new AsyncAtomicFactory<int, int>(); |
147 | | - |
148 | | - a.Equals(b).Should().BeFalse(); |
149 | | - } |
150 | | - |
151 | | - [Fact] |
152 | | - public void WhenArgNullEqualsFalse() |
153 | | - { |
154 | | - new AsyncAtomicFactory<int, int>(1) |
155 | | - .Equals(null) |
156 | | - .Should().BeFalse(); |
157 | | - } |
158 | | - |
159 | | - [Fact] |
160 | | - public void WhenArgObjectValuesAreSameEqualsTrue() |
161 | | - { |
162 | | - object other = new AsyncAtomicFactory<int, int>(1); |
163 | | - |
164 | | - new AsyncAtomicFactory<int, int>(1) |
165 | | - .Equals(other) |
166 | | - .Should().BeTrue(); |
167 | | - } |
168 | | - } |
169 | | -} |
| 142 | + [Fact] |
| 143 | + public void WhenOtherValueNotCreatedEqualsFalse() |
| 144 | + { |
| 145 | + var a = new AsyncAtomicFactory<int, int>(1); |
| 146 | + var b = new AsyncAtomicFactory<int, int>(); |
| 147 | + |
| 148 | + a.Equals(b).Should().BeFalse(); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void WhenArgNullEqualsFalse() |
| 153 | + { |
| 154 | + new AsyncAtomicFactory<int, int>(1) |
| 155 | + .Equals(null) |
| 156 | + .Should().BeFalse(); |
| 157 | + } |
| 158 | + |
| 159 | + [Fact] |
| 160 | + public void WhenArgObjectValuesAreSameEqualsTrue() |
| 161 | + { |
| 162 | + object other = new AsyncAtomicFactory<int, int>(1); |
| 163 | + |
| 164 | + new AsyncAtomicFactory<int, int>(1) |
| 165 | + .Equals(other) |
| 166 | + .Should().BeTrue(); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments