// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Net; using System.Threading.Tasks; using StackExchange.Redis; namespace Microsoft.AspNetCore.SignalR.Tests { public class TestConnectionMultiplexer : IConnectionMultiplexer { public string ClientName => throw new NotImplementedException(); public string Configuration => throw new NotImplementedException(); public int TimeoutMilliseconds => throw new NotImplementedException(); public long OperationCount => throw new NotImplementedException(); public bool PreserveAsyncOrder { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public bool IsConnected => true; public bool IncludeDetailInExceptions { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public int StormLogThreshold { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public event EventHandler ErrorMessage { add { } remove { } } public event EventHandler ConnectionFailed { add { } remove { } } public event EventHandler InternalError { add { } remove { } } public event EventHandler ConnectionRestored { add { } remove { } } public event EventHandler ConfigurationChanged { add { } remove { } } public event EventHandler ConfigurationChangedBroadcast { add { } remove { } } public event EventHandler HashSlotMoved { add { } remove { } } private readonly ISubscriber _subscriber; public TestConnectionMultiplexer(TestRedisServer server) { _subscriber = new TestSubscriber(server); } public void BeginProfiling(object forContext) { throw new NotImplementedException(); } public void Close(bool allowCommandsToComplete = true) { throw new NotImplementedException(); } public Task CloseAsync(bool allowCommandsToComplete = true) { throw new NotImplementedException(); } public bool Configure(TextWriter log = null) { throw new NotImplementedException(); } public Task ConfigureAsync(TextWriter log = null) { throw new NotImplementedException(); } public void Dispose() { throw new NotImplementedException(); } public ProfiledCommandEnumerable FinishProfiling(object forContext, bool allowCleanupSweep = true) { throw new NotImplementedException(); } public ServerCounters GetCounters() { throw new NotImplementedException(); } public IDatabase GetDatabase(int db = -1, object asyncState = null) { throw new NotImplementedException(); } public EndPoint[] GetEndPoints(bool configuredOnly = false) { throw new NotImplementedException(); } public IServer GetServer(string host, int port, object asyncState = null) { throw new NotImplementedException(); } public IServer GetServer(string hostAndPort, object asyncState = null) { throw new NotImplementedException(); } public IServer GetServer(IPAddress host, int port) { throw new NotImplementedException(); } public IServer GetServer(EndPoint endpoint, object asyncState = null) { throw new NotImplementedException(); } public string GetStatus() { throw new NotImplementedException(); } public void GetStatus(TextWriter log) { throw new NotImplementedException(); } public string GetStormLog() { throw new NotImplementedException(); } public ISubscriber GetSubscriber(object asyncState = null) { return _subscriber; } public int HashSlot(RedisKey key) { throw new NotImplementedException(); } public long PublishReconfigure(CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public Task PublishReconfigureAsync(CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public void RegisterProfiler(IProfiler profiler) { throw new NotImplementedException(); } public void ResetStormLog() { throw new NotImplementedException(); } public void Wait(Task task) { throw new NotImplementedException(); } public T Wait(Task task) { throw new NotImplementedException(); } public void WaitAll(params Task[] tasks) { throw new NotImplementedException(); } } public class TestRedisServer { private readonly ConcurrentDictionary>> _subscriptions = new ConcurrentDictionary>>(); public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None) { if (_subscriptions.TryGetValue(channel, out var handlers)) { foreach (var handler in handlers) { handler(channel, message); } } return handlers != null ? handlers.Count : 0; } public void Subscribe(RedisChannel channel, Action handler, CommandFlags flags = CommandFlags.None) { _subscriptions.AddOrUpdate(channel, _ => new List> { handler }, (_, list) => { list.Add(handler); return list; }); } public void Unsubscribe(RedisChannel channel, Action handler = null, CommandFlags flags = CommandFlags.None) { if (_subscriptions.TryGetValue(channel, out var list)) { list.Remove(handler); } } } public class TestSubscriber : ISubscriber { private readonly TestRedisServer _server; public ConnectionMultiplexer Multiplexer => throw new NotImplementedException(); public TestSubscriber(TestRedisServer server) { _server = server; } public EndPoint IdentifyEndpoint(RedisChannel channel, CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public Task IdentifyEndpointAsync(RedisChannel channel, CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public bool IsConnected(RedisChannel channel = default) { throw new NotImplementedException(); } public TimeSpan Ping(CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public Task PingAsync(CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None) { return _server.Publish(channel, message, flags); } public async Task PublishAsync(RedisChannel channel, RedisValue message, CommandFlags flags = CommandFlags.None) { await Task.Yield(); return Publish(channel, message, flags); } public void Subscribe(RedisChannel channel, Action handler, CommandFlags flags = CommandFlags.None) { _server.Subscribe(channel, handler, flags); } public Task SubscribeAsync(RedisChannel channel, Action handler, CommandFlags flags = CommandFlags.None) { Subscribe(channel, handler, flags); return Task.CompletedTask; } public EndPoint SubscribedEndpoint(RedisChannel channel) { throw new NotImplementedException(); } public bool TryWait(Task task) { throw new NotImplementedException(); } public void Unsubscribe(RedisChannel channel, Action handler = null, CommandFlags flags = CommandFlags.None) { _server.Unsubscribe(channel, handler, flags); } public void UnsubscribeAll(CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public Task UnsubscribeAllAsync(CommandFlags flags = CommandFlags.None) { throw new NotImplementedException(); } public Task UnsubscribeAsync(RedisChannel channel, Action handler = null, CommandFlags flags = CommandFlags.None) { Unsubscribe(channel, handler, flags); return Task.CompletedTask; } public void Wait(Task task) { throw new NotImplementedException(); } public T Wait(Task task) { throw new NotImplementedException(); } public void WaitAll(params Task[] tasks) { throw new NotImplementedException(); } } }