/// <summary> /// Removes a queue from the channel /// </summary> public async Task RemoveQueue(ChannelQueue queue) { _queues.Remove(queue); await queue.SetStatus(QueueStatus.Stopped); if (EventHandler != null) { await EventHandler.OnQueueRemoved(queue, this); } await queue.Destroy(); }
public async Task PushToLateClients() { TestMqServer server = new TestMqServer(); server.Initialize(42506); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:42506"); Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); await queue.SetStatus(QueueStatus.Push); bool received = false; client.MessageReceived += (c, m) => { if (m.Type == MessageType.Channel) { received = true; } }; MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, false); Assert.True(sent); await Task.Delay(1500); Assert.NotEmpty(queue.RegularMessages); Assert.False(received); TmqResponseCode joined = await client.Join(channel.Name, true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(1500); Assert.Empty(queue.RegularMessages); Assert.True(received); }
/// <summary> /// Creates new queue and sends response /// </summary> private async Task UpdateQueue(MqClient client, TmqMessage message) { if (_server.AdminAuthorization == null) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized)); } return; } NetworkOptionsBuilder builder = new NetworkOptionsBuilder(); builder.Load(message.ToString()); Channel channel = _server.FindChannel(message.Target); if (channel == null) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound)); } return; } ChannelQueue queue = channel.FindQueue(builder.Id); if (queue == null) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound)); } return; } bool grant = await _server.AdminAuthorization.CanUpdateQueueOptions(client, channel, queue, builder); if (!grant) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized)); } return; } builder.ApplyToQueue(queue.Options); if (builder.Status.HasValue) { await queue.SetStatus(builder.Status.Value); } //if creation successful, sends response if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok)); } }
public async Task SendOnlyFirstLateClients() { TestMqServer server = new TestMqServer(); server.Initialize(42509); server.Start(); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.SendOnlyFirstAcquirer = true; await queue.SetStatus(QueueStatus.Push); bool[] received = new bool[3]; async Task <TmqClient> join(int no) { TmqClient client = new TmqClient(); client.ClientId = "client-" + no; await client.ConnectAsync("tmq://localhost:42509"); Assert.True(client.IsConnected); client.MessageReceived += (cx, m) => { if (m.Type == MessageType.Channel) { received[no - 1] = true; } }; return(client); } TmqClient client1 = await join(1); TmqClient client2 = await join(2); TmqClient client3 = await join(3); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client1.Push(channel.Name, queue.Id, ms, false); Assert.True(sent); await Task.Delay(1500); TmqResponseCode joined = await client1.Join(channel.Name, true); Assert.Equal(TmqResponseCode.Ok, joined); joined = await client2.Join(channel.Name, true); Assert.Equal(TmqResponseCode.Ok, joined); joined = await client3.Join(channel.Name, true); Assert.Equal(TmqResponseCode.Ok, joined); await Task.Delay(250); Assert.Empty(queue.HighPriorityMessages); Assert.Empty(queue.RegularMessages); int c = received.Count(x => x); Assert.Equal(1, c); }