public async override Task ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, byte[] buffer) { var reader = new StreamReader(new MemoryStream(buffer), Encoding.Default); var msg = new JsonSerializer().Deserialize <GunMessage>(new JsonTextReader(reader)); if (_duplicateManager.Check(msg.Key)) { return; } _duplicateManager.Track(msg.Key); if (msg is PutMessage) { var change = _graph.Mix(((PutMessage)msg).PutChanges); } if (msg is GetMessage) { var ack = _graph.Get(((GetMessage)msg).Get); if (ack != null) { var response = new PutMessage() { Key = _duplicateManager.Track(DuplicateManager.Random()), At = msg.Key, PutChanges = ack }; await this.SendMessageToAllAsync(JsonConvert.SerializeObject(response)); } } await this.SendMessageToAllAsync(JsonConvert.SerializeObject(msg)); }
public void ShouldCheckFalseWithNotExist() { var dup = new DuplicateManager(Options.Create <DuplicateManagerOptions>(new DuplicateManagerOptions())); var key = DuplicateManager.Random(); var res = dup.Check(key); Assert.False(false); }
public void ShouldTrack() { var dup = new DuplicateManager(Options.Create <DuplicateManagerOptions>(new DuplicateManagerOptions())); var key = DuplicateManager.Random(); var res = dup.Track(key); Assert.Equal(key, res); }
public void ShouldCheckTrueWithDuplicate() { var dup = new DuplicateManager(Options.Create <DuplicateManagerOptions>(new DuplicateManagerOptions())); var key = DuplicateManager.Random(); var res = dup.Check(key); var res2 = dup.Track(key); var res3 = dup.Check(key); Assert.False(res); Assert.True(res3); }
public async void ShouldCheckFalseAfterTimeout() { var dup = new DuplicateManager(Options.Create <DuplicateManagerOptions>(new DuplicateManagerOptions())); var key = DuplicateManager.Random(); var res = dup.Check(key); var res2 = dup.Track(key); await Task.Delay(1000 * 10); var res3 = dup.Check(key); Assert.False(res); Assert.False(res3); }
public void ShouldRandom() { var res = DuplicateManager.Random(); Assert.Equal(3, res.Length); }