-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionObjectPoolSpec.cs
More file actions
107 lines (84 loc) · 2.93 KB
/
Copy pathConnectionObjectPoolSpec.cs
File metadata and controls
107 lines (84 loc) · 2.93 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
using GaudiHTTP.Pooling;
namespace GaudiHTTP.Tests.Pooling;
public sealed class ConnectionObjectPoolSpec
{
private sealed class Counter : IResetable
{
public int Value;
public int ResetCount;
public void Reset()
{
Value = 0;
ResetCount++;
}
public void Dispose() => ConnectionObjectPool.Instance.Return(this);
}
private sealed class PoolableCounter : Poolable<PoolableCounter>
{
public int Value;
public int ResetCount;
protected override void OnReset()
{
Value = 0;
ResetCount++;
}
}
[Fact(Timeout = 5000)]
public void Rent_after_return_reuses_the_same_reset_instance()
{
var ctx = ConnectionObjectPool.Instance;
var a = ctx.Rent(static () => new Counter());
a.Value = 42;
ctx.Return(a);
var b = ctx.Rent(static () => new Counter());
Assert.Same(a, b);
Assert.Equal(0, b.Value);
// Drain so this test doesn't leak a pooled Counter into other tests sharing the singleton.
ctx.Return(b);
}
[Fact(Timeout = 5000)]
public void Dispose_on_a_Poolable_returns_it_to_the_singleton_pool_reset()
{
var ctx = ConnectionObjectPool.Instance;
var a = ctx.Rent(static () => new PoolableCounter());
a.Value = 7;
a.Dispose();
var b = ctx.Rent(static () => new PoolableCounter());
Assert.Same(a, b);
Assert.Equal(0, b.Value);
ctx.Return(b);
}
private sealed class ReturnFirstCounter : Poolable<ReturnFirstCounter>
{
public int Value;
protected override void OnReset() => Value = 0;
}
[Fact(Timeout = 5000)]
public void Return_before_any_rent_must_not_throw_and_later_rent_reuses_the_instance()
{
// A pooled object constructed directly (new, not rented) disposes itself back into the
// pool. That Return may be the very first pool touch for the type — it must not require
// a factory registration.
var ctx = ConnectionObjectPool.Instance;
var a = new ReturnFirstCounter { Value = 5 };
a.Dispose();
var b = ctx.Rent(static () => new ReturnFirstCounter());
Assert.Same(a, b);
Assert.Equal(0, b.Value);
ctx.Return(b);
}
[Fact(Timeout = 5000)]
public void Double_dispose_on_a_Poolable_is_a_safe_no_op()
{
var ctx = ConnectionObjectPool.Instance;
var a = ctx.Rent(static () => new PoolableCounter());
a.Dispose();
var resetCountAfterFirstDispose = a.ResetCount;
// A second Dispose must not return the instance to the pool a second time (which would
// otherwise let two live rentals alias the same object).
a.Dispose();
Assert.Equal(resetCountAfterFirstDispose, a.ResetCount);
var b = ctx.Rent(static () => new PoolableCounter());
ctx.Return(b);
}
}