|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Net; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using StackExchange.Redis.Server; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace StackExchange.Redis.Tests; |
| 10 | + |
| 11 | +public class Resp3HandshakeTests(ITestOutputHelper log) |
| 12 | +{ |
| 13 | + public enum ServerResponse |
| 14 | + { |
| 15 | + Resp3, // up-level server style |
| 16 | + Resp2, // DMC hybrid style, i.e. we know about it, but: "no, you'll take RESP2" |
| 17 | + UnknownCommand, // down-level server style |
| 18 | + } |
| 19 | + |
| 20 | + [Flags] |
| 21 | + public enum HandshakeFlags |
| 22 | + { |
| 23 | + None = 0, |
| 24 | + Authenticated = 1 << 0, |
| 25 | + TieBreaker = 1 << 1, |
| 26 | + ConfigChannel = 1 << 2, |
| 27 | + UsePubSub = 1 << 3, |
| 28 | + UseDatabase = 1 << 4, |
| 29 | + } |
| 30 | + |
| 31 | + private static readonly int HandshakeFlagsCount = Enum.GetValues(typeof(HandshakeFlags)).Length - 1; |
| 32 | + public static IEnumerable<object[]> GetHandshakeParameters() |
| 33 | + { |
| 34 | + // all client protocols, all server-response modes; all flag permutations |
| 35 | + var clients = (RedisProtocol[])Enum.GetValues(typeof(RedisProtocol)); |
| 36 | + var servers = (ServerResponse[])Enum.GetValues(typeof(ServerResponse)); |
| 37 | + foreach (var client in clients) |
| 38 | + { |
| 39 | + foreach (var server in servers) |
| 40 | + { |
| 41 | + if (client is RedisProtocol.Resp2 & server is not ServerResponse.Resp2) |
| 42 | + { |
| 43 | + // we don't issue HELLO for this, nothing to test |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + int count = 1 << HandshakeFlagsCount; |
| 48 | + for (int i = 0; i < count; i++) |
| 49 | + { |
| 50 | + yield return [client, server, (HandshakeFlags)i]; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [Theory] |
| 58 | + [MemberData(nameof(GetHandshakeParameters))] |
| 59 | + public async Task Handshake(RedisProtocol client, ServerResponse server, HandshakeFlags flags) |
| 60 | + { |
| 61 | + using var serverObj = new HandshakeServer(server, log); |
| 62 | + serverObj.Password = (flags & HandshakeFlags.Authenticated) == 0 ? null : "mypassword"; |
| 63 | + var config = serverObj.GetClientConfig(); |
| 64 | + config.Protocol = client; |
| 65 | + config.TieBreaker = (flags & HandshakeFlags.TieBreaker) == 0 ? "" : "tiebreaker_key"; |
| 66 | + config.ConfigurationChannel = (flags & HandshakeFlags.ConfigChannel) == 0 ? "" : "broadcast_channel"; |
| 67 | + |
| 68 | + using var clientObj = await ConnectionMultiplexer.ConnectAsync(config); |
| 69 | + |
| 70 | + var sub = clientObj.GetSubscriber(); |
| 71 | + var db = clientObj.GetDatabase(); |
| 72 | + ConcurrentBag<string> received = []; |
| 73 | + RedisChannel channel = RedisChannel.Literal("mychannel"); |
| 74 | + RedisKey key = "mykey"; |
| 75 | + bool useDatabase = (flags & HandshakeFlags.UseDatabase) != 0; |
| 76 | + bool usePubSub = (flags & HandshakeFlags.UsePubSub) != 0; |
| 77 | + |
| 78 | + if (usePubSub) |
| 79 | + { |
| 80 | + await sub.SubscribeAsync(channel, (x, y) => received.Add(y!)); |
| 81 | + } |
| 82 | + if (useDatabase) |
| 83 | + { |
| 84 | + await db.StringSetAsync(key, "myvalue"); |
| 85 | + } |
| 86 | + if (usePubSub) |
| 87 | + { |
| 88 | + await sub.PublishAsync(channel, "msg payload"); |
| 89 | + for (int i = 0; i < 5 && received.IsEmpty; i++) |
| 90 | + { |
| 91 | + await Task.Delay(10, TestContext.Current.CancellationToken); |
| 92 | + await sub.PingAsync(); |
| 93 | + } |
| 94 | + Assert.Equal("msg payload", Assert.Single(received)); |
| 95 | + } |
| 96 | + |
| 97 | + if (useDatabase) |
| 98 | + { |
| 99 | + Assert.Equal("myvalue", await db.StringGetAsync(key)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static readonly EndPoint EP = new DnsEndPoint("home", 8000); |
| 104 | + private sealed class HandshakeServer(ServerResponse response, ITestOutputHelper log) |
| 105 | + : InProcessTestServer(log, EP) |
| 106 | + { |
| 107 | + protected override RedisProtocol MaxProtocol => response switch |
| 108 | + { |
| 109 | + ServerResponse.Resp3 => RedisProtocol.Resp3, |
| 110 | + _ => RedisProtocol.Resp2, |
| 111 | + }; |
| 112 | + |
| 113 | + protected override TypedRedisValue Hello(RedisClient client, in RedisRequest request) |
| 114 | + => response is ServerResponse.UnknownCommand |
| 115 | + ? request.CommandNotFound() |
| 116 | + : base.Hello(client, in request); |
| 117 | + } |
| 118 | +} |
0 commit comments