Skip to content

Commit a93d26d

Browse files
committed
migrate basic-ops in-proc tests to use new factory;
1 parent bc7124d commit a93d26d

6 files changed

Lines changed: 44 additions & 60 deletions

File tree

tests/StackExchange.Redis.Tests/BasicOpTests.cs

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,26 @@ public abstract class BasicOpsTestsBase(ITestOutputHelper output, SharedConnecti
2525
[Fact]
2626
public async Task PingOnce()
2727
{
28-
await using var conn = Create();
28+
await using var conn = ConnectFactory();
2929
var db = conn.GetDatabase();
3030

3131
var duration = await db.PingAsync().ForAwait();
3232
Log("Ping took: " + duration);
3333
Assert.True(duration.TotalMilliseconds > 0);
3434
}
3535

36-
[Fact(Skip = "This needs some CI love, it's not a scenario we care about too much but noisy atm.")]
36+
[Fact]
3737
public async Task RapidDispose()
3838
{
39-
await using var primary = Create();
39+
SkipIfWouldUseRealServer("This needs some CI love, it's not a scenario we care about too much but noisy atm.");
40+
await using var primary = ConnectFactory();
4041
var db = primary.GetDatabase();
4142
RedisKey key = Me();
4243
db.KeyDelete(key, CommandFlags.FireAndForget);
4344

4445
for (int i = 0; i < 10; i++)
4546
{
46-
await using var secondary = Create(fail: true, shared: false);
47+
await using var secondary = primary.CreateClient();
4748
secondary.GetDatabase().StringIncrement(key, flags: CommandFlags.FireAndForget);
4849
}
4950
// Give it a moment to get through the pipe...they were fire and forget
@@ -54,7 +55,7 @@ public async Task RapidDispose()
5455
[Fact]
5556
public async Task PingMany()
5657
{
57-
await using var conn = Create();
58+
await using var conn = ConnectFactory();
5859
var db = conn.GetDatabase();
5960
var tasks = new Task<TimeSpan>[100];
6061
for (int i = 0; i < tasks.Length; i++)
@@ -69,7 +70,7 @@ public async Task PingMany()
6970
[Fact]
7071
public async Task GetWithNullKey()
7172
{
72-
await using var conn = Create();
73+
await using var conn = ConnectFactory();
7374
var db = conn.GetDatabase();
7475
const string? key = null;
7576
var ex = Assert.Throws<ArgumentException>(() => db.StringGet(key));
@@ -79,7 +80,7 @@ public async Task GetWithNullKey()
7980
[Fact]
8081
public async Task SetWithNullKey()
8182
{
82-
await using var conn = Create();
83+
await using var conn = ConnectFactory();
8384
var db = conn.GetDatabase();
8485
const string? key = null, value = "abc";
8586
var ex = Assert.Throws<ArgumentException>(() => db.StringSet(key!, value));
@@ -89,7 +90,7 @@ public async Task SetWithNullKey()
8990
[Fact]
9091
public async Task SetWithNullValue()
9192
{
92-
await using var conn = Create();
93+
await using var conn = ConnectFactory();
9394
var db = conn.GetDatabase();
9495
string key = Me();
9596
const string? value = null;
@@ -107,7 +108,7 @@ public async Task SetWithNullValue()
107108
[Fact]
108109
public async Task SetWithDefaultValue()
109110
{
110-
await using var conn = Create();
111+
await using var conn = ConnectFactory();
111112
var db = conn.GetDatabase();
112113
string key = Me();
113114
var value = default(RedisValue); // this is kinda 0... ish
@@ -125,7 +126,7 @@ public async Task SetWithDefaultValue()
125126
[Fact]
126127
public async Task SetWithZeroValue()
127128
{
128-
await using var conn = Create();
129+
await using var conn = ConnectFactory();
129130
var db = conn.GetDatabase();
130131
string key = Me();
131132
const long value = 0;
@@ -143,7 +144,7 @@ public async Task SetWithZeroValue()
143144
[Fact]
144145
public async Task GetSetAsync()
145146
{
146-
await using var conn = Create();
147+
await using var conn = ConnectFactory();
147148
var db = conn.GetDatabase();
148149

149150
RedisKey key = Me();
@@ -168,7 +169,7 @@ public async Task GetSetAsync()
168169
[Fact]
169170
public async Task GetSetSync()
170171
{
171-
await using var conn = Create();
172+
await using var conn = ConnectFactory();
172173
var db = conn.GetDatabase();
173174

174175
RedisKey key = Me();
@@ -195,7 +196,7 @@ public async Task GetSetSync()
195196
[InlineData(true, false)]
196197
public async Task GetWithExpiry(bool exists, bool hasExpiry)
197198
{
198-
await using var conn = Create();
199+
await using var conn = ConnectFactory();
199200
var db = conn.GetDatabase();
200201
RedisKey key = Me();
201202
db.KeyDelete(key, CommandFlags.FireAndForget);
@@ -231,7 +232,7 @@ public async Task GetWithExpiry(bool exists, bool hasExpiry)
231232
[Fact]
232233
public async Task GetWithExpiryWrongTypeAsync()
233234
{
234-
await using var conn = Create();
235+
await using var conn = ConnectFactory();
235236
var db = conn.GetDatabase();
236237
RedisKey key = Me();
237238
_ = db.KeyDeleteAsync(key);
@@ -254,7 +255,7 @@ public async Task GetWithExpiryWrongTypeAsync()
254255
[Fact]
255256
public async Task GetWithExpiryWrongTypeSync()
256257
{
257-
await using var conn = Create();
258+
await using var conn = ConnectFactory();
258259
var db = conn.GetDatabase();
259260
RedisKey key = Me();
260261
var ex = await Assert.ThrowsAsync<RedisServerException>(async () =>
@@ -270,12 +271,12 @@ public async Task GetWithExpiryWrongTypeSync()
270271
[Fact]
271272
public async Task TestSevered()
272273
{
273-
await using var conn = Create(allowAdmin: true, shared: false);
274+
await using var conn = ConnectFactory(allowAdmin: true, shared: false);
274275
var db = conn.GetDatabase();
275276
string key = Me();
276277
db.KeyDelete(key, CommandFlags.FireAndForget);
277278
db.StringSet(key, key, flags: CommandFlags.FireAndForget);
278-
var server = GetServer(conn);
279+
var server = GetServer(conn.DefaultClient);
279280
Assert.SkipUnless(server.CanSimulateConnectionFailure(), "Skipping because server cannot simulate connection failure");
280281

281282
SetExpectedAmbientFailureCount(2);
@@ -293,7 +294,7 @@ public async Task TestSevered()
293294
[Fact]
294295
public async Task IncrAsync()
295296
{
296-
await using var conn = Create();
297+
await using var conn = ConnectFactory();
297298
var db = conn.GetDatabase();
298299
RedisKey key = Me();
299300
db.KeyDelete(key, CommandFlags.FireAndForget);
@@ -321,7 +322,7 @@ public async Task IncrAsync()
321322
[Fact]
322323
public async Task IncrSync()
323324
{
324-
await using var conn = Create();
325+
await using var conn = ConnectFactory();
325326
var db = conn.GetDatabase();
326327
RedisKey key = Me();
327328
Log(key);
@@ -350,7 +351,7 @@ public async Task IncrSync()
350351
[Fact]
351352
public async Task IncrDifferentSizes()
352353
{
353-
await using var conn = Create();
354+
await using var conn = ConnectFactory();
354355
var db = conn.GetDatabase();
355356
RedisKey key = Me();
356357
db.KeyDelete(key, CommandFlags.FireAndForget);
@@ -380,30 +381,10 @@ private static void Incr(IDatabase database, RedisKey key, int delta, ref int to
380381
total += delta;
381382
}
382383

383-
[Fact]
384-
public async Task ShouldUseSharedMuxer()
385-
{
386-
Log($"Shared: {SharedFixtureAvailable}");
387-
if (SharedFixtureAvailable)
388-
{
389-
await using var a = Create();
390-
Assert.IsNotType<ConnectionMultiplexer>(a);
391-
await using var b = Create();
392-
Assert.Same(a, b);
393-
}
394-
else
395-
{
396-
await using var a = Create();
397-
Assert.IsType<ConnectionMultiplexer>(a);
398-
await using var b = Create();
399-
Assert.NotSame(a, b);
400-
}
401-
}
402-
403384
[Fact]
404385
public async Task Delete()
405386
{
406-
await using var conn = Create();
387+
await using var conn = ConnectFactory();
407388
var db = conn.GetDatabase();
408389
var key = Me();
409390
_ = db.StringSetAsync(key, "Heyyyyy");
@@ -418,7 +399,7 @@ public async Task Delete()
418399
[Fact]
419400
public async Task DeleteAsync()
420401
{
421-
await using var conn = Create();
402+
await using var conn = ConnectFactory();
422403
var db = conn.GetDatabase();
423404
var key = Me();
424405
_ = db.StringSetAsync(key, "Heyyyyy");
@@ -433,7 +414,7 @@ public async Task DeleteAsync()
433414
[Fact]
434415
public async Task DeleteMany()
435416
{
436-
await using var conn = Create();
417+
await using var conn = ConnectFactory();
437418
var db = conn.GetDatabase();
438419
var key1 = Me();
439420
var key2 = Me() + "2";
@@ -452,7 +433,7 @@ public async Task DeleteMany()
452433
[Fact]
453434
public async Task DeleteManyAsync()
454435
{
455-
await using var conn = Create();
436+
await using var conn = ConnectFactory();
456437
var db = conn.GetDatabase();
457438
var key1 = Me();
458439
var key2 = Me() + "2";
@@ -472,7 +453,7 @@ public async Task DeleteManyAsync()
472453
public async Task WrappedDatabasePrefixIntegration()
473454
{
474455
var key = Me();
475-
await using var conn = Create();
456+
await using var conn = ConnectFactory();
476457
var db = conn.GetDatabase().WithKeyPrefix("abc");
477458
db.KeyDelete(key, CommandFlags.FireAndForget);
478459
db.StringIncrement(key, flags: CommandFlags.FireAndForget);
@@ -486,8 +467,8 @@ public async Task WrappedDatabasePrefixIntegration()
486467
[Fact]
487468
public async Task TransactionSync()
488469
{
489-
await using var conn = Create();
490-
Assert.SkipUnless(conn.RawConfig.CommandMap.IsAvailable(RedisCommand.MULTI), "MULTI is not available");
470+
await using var conn = ConnectFactory();
471+
Assert.SkipUnless(conn.DefaultClient.RawConfig.CommandMap.IsAvailable(RedisCommand.MULTI), "MULTI is not available");
491472
var db = conn.GetDatabase();
492473

493474
RedisKey key = Me();
@@ -506,8 +487,8 @@ public async Task TransactionSync()
506487
[Fact]
507488
public async Task TransactionAsync()
508489
{
509-
await using var conn = Create();
510-
Assert.SkipUnless(conn.RawConfig.CommandMap.IsAvailable(RedisCommand.MULTI), "MULTI is not available");
490+
await using var conn = ConnectFactory();
491+
Assert.SkipUnless(conn.DefaultClient.RawConfig.CommandMap.IsAvailable(RedisCommand.MULTI), "MULTI is not available");
511492

512493
var db = conn.GetDatabase();
513494

tests/StackExchange.Redis.Tests/ExceptionFactoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace StackExchange.Redis.Tests;
66

7-
public class ExceptionFactoryTests(ITestOutputHelper output) : TestBase(output)
7+
public class ExceptionFactoryTests(ITestOutputHelper output, InProcServerFixture fixture) : TestBase(output, fixture)
88
{
99
[Fact]
1010
public async Task NullLastException()

tests/StackExchange.Redis.Tests/InProcessTestServer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Pipelines.Sockets.Unofficial;
109
using StackExchange.Redis.Configuration;
1110
using StackExchange.Redis.Server;
1211
using Xunit;
@@ -25,7 +24,7 @@ public InProcessTestServer(ITestOutputHelper? log = null)
2524
Tunnel = new InProcTunnel(this);
2625
}
2726

28-
public Task<ConnectionMultiplexer> ConnectAsync(bool withPubSub = false, WriteMode writeMode = WriteMode.Default, TextWriter? log = null)
27+
public Task<ConnectionMultiplexer> ConnectAsync(bool withPubSub = true, WriteMode writeMode = WriteMode.Default, TextWriter? log = null)
2928
=> ConnectionMultiplexer.ConnectAsync(GetClientConfig(withPubSub, writeMode), log);
3029

3130
// view request/response highlights in the log
@@ -47,7 +46,7 @@ public override TypedRedisValue Execute(RedisClient client, in RedisRequest requ
4746
return result;
4847
}
4948

50-
public ConfigurationOptions GetClientConfig(bool withPubSub = false, WriteMode writeMode = WriteMode.Default)
49+
public ConfigurationOptions GetClientConfig(bool withPubSub = true, WriteMode writeMode = WriteMode.Default)
5150
{
5251
var commands = GetCommands();
5352
if (!withPubSub)

tests/StackExchange.Redis.Tests/MovedUnitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task CrossSlotDisallowed(ServerType serverType, WriteMode writeMode
2727
string keyA = "abc", keyB = "def"; // known to be on different slots
2828

2929
using var server = new InProcessTestServer(log) { ServerType = serverType };
30-
await using var muxer = await server.ConnectAsync(writeMode: writeMode);
30+
await using var muxer = await server.ConnectAsync(writeMode: writeMode, withPubSub: false);
3131

3232
var db = muxer.GetDatabase();
3333
await db.StringSetAsync(keyA, "value", flags: CommandFlags.FireAndForget);
@@ -109,7 +109,7 @@ public async Task MovedToSameEndpoint_TriggersReconnectAndRetry_CommandSucceeds(
109109
log: log) { ServerType = serverType, };
110110

111111
// Act: Connect to the test server
112-
await using var conn = await testServer.ConnectAsync();
112+
await using var conn = await testServer.ConnectAsync(withPubSub: false);
113113
// Ping the server to ensure it's responsive
114114
var server = conn.GetServer(testServer.DefaultEndPoint);
115115

tests/StackExchange.Redis.Tests/PubSubTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class InProcPubSubTests(ITestOutputHelper output, InProcServerFixture fix
2121
: PubSubTestBase(output, null, fixture)
2222
{
2323
protected override bool UseDedicatedInProcessServer => true;
24-
protected override bool UseInProcessServerPubSub => true;
2524
}
2625

2726
[RunPerProtocol]

tests/StackExchange.Redis.Tests/TestBase.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ protected static async Task UntilConditionAsync(TimeSpan maxWaitTime, Func<bool>
586586

587587
// simplified usage to get an interchangeable dedicated vs shared in-process server, useful for debugging
588588
protected virtual bool UseDedicatedInProcessServer => false; // use the shared server by default
589-
protected virtual bool UseInProcessServerPubSub => false;
589+
590590
internal ClientFactory ConnectFactory(bool allowAdmin = false, string? channelPrefix = null, bool shared = true)
591591
{
592592
if (UseDedicatedInProcessServer)
@@ -597,9 +597,14 @@ internal ClientFactory ConnectFactory(bool allowAdmin = false, string? channelPr
597597
return new ClientFactory(this, allowAdmin, channelPrefix, shared, null);
598598
}
599599

600-
protected void SkipIfWouldUseInProcessServer()
600+
protected void SkipIfWouldUseInProcessServer(string? reason = null)
601+
{
602+
Assert.SkipWhen(_inProcServerFixture != null || UseDedicatedInProcessServer, reason ?? "In-process server is in use.");
603+
}
604+
605+
protected void SkipIfWouldUseRealServer(string? reason = null)
601606
{
602-
Assert.SkipWhen(_inProcServerFixture != null || UseDedicatedInProcessServer, "In-process server is in use.");
607+
Assert.SkipUnless(_inProcServerFixture != null || UseDedicatedInProcessServer, reason ?? "Real server is in use.");
603608
}
604609

605610
internal sealed class ClientFactory : IDisposable, IAsyncDisposable
@@ -628,7 +633,7 @@ public IInternalConnectionMultiplexer CreateClient()
628633
{
629634
if (_server is not null)
630635
{
631-
var config = _server.GetClientConfig(withPubSub: _testBase.UseInProcessServerPubSub);
636+
var config = _server.GetClientConfig();
632637
config.AllowAdmin = _allowAdmin;
633638
config.Protocol = TestContext.Current.GetProtocol();
634639
if (_channelPrefix is not null)

0 commit comments

Comments
 (0)