public async Task CreateWithProperties() { TestMqServer server = new TestMqServer(); server.Initialize(41206); server.Start(); TmqClient client = new TmqClient(); await client.ConnectAsync("tmq://localhost:41206"); Assert.True(client.IsConnected); TmqResponseCode created = await client.CreateQueue("ch-test", MessageA.ContentType, true, o => { o.SendOnlyFirstAcquirer = true; o.AcknowledgeTimeout = TimeSpan.FromSeconds(33); o.Status = MessagingQueueStatus.Pull; }); Assert.Equal(TmqResponseCode.Ok, created); Channel channel = server.Server.FindChannel("ch-test"); Assert.NotNull(channel); ChannelQueue queue = channel.FindQueue(MessageA.ContentType); Assert.NotNull(queue); Assert.True(queue.Options.SendOnlyFirstAcquirer); Assert.Equal(TimeSpan.FromSeconds(33), queue.Options.AcknowledgeTimeout); Assert.Equal(QueueStatus.Pull, queue.Status); }
private static async Task ListenToQueue(ChannelQueue <PersonProper> channel, CancellationToken token) { await foreach (var item in channel.ListenAsync(token)) { Trace.WriteLine(item.Email); } }
public async Task Remove(bool verifyResponse) { int port = 41208 + Convert.ToInt32(verifyResponse); TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(); Channel channel = server.Server.FindChannel("ch-route"); Assert.NotNull(channel); ChannelQueue queue = channel.FindQueue(MessageA.ContentType); Assert.NotNull(queue); TmqAdminClient client = new TmqAdminClient(); await client.ConnectAsync("tmq://localhost:" + port); Assert.True(client.IsConnected); TmqResponseCode done = await client.RemoveQueue("ch-route", MessageA.ContentType, verifyResponse); Assert.Equal(TmqResponseCode.Ok, done); if (!verifyResponse) { await Task.Delay(500); } queue = channel.FindQueue(MessageA.ContentType); Assert.Null(queue); }
public async Task Create(bool verifyResponse) { int port = verifyResponse ? 40905 : 40904; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(); TmqClient client = new TmqClient(); client.Connect("tmq://localhost:" + port); TmqResponseCode created = await client.CreateQueue("ch-2", MessageA.ContentType, verifyResponse); Assert.Equal(TmqResponseCode.Ok, created); await Task.Delay(1000); Channel channel = server.Server.Channels.FirstOrDefault(x => x.Name == "ch-2"); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); Assert.Equal(MessageA.ContentType, queue.Id); }
public AddMovieCommandHandler(WriteMovieRepository movieRepository, DirectorRepository directorRepository, ApplicationDbContext db, ChannelQueue <MovieAdded> channel) { _movieRepository = movieRepository; _directorRepository = directorRepository; _db = db; _channel = channel; }
internal static IQueueState Create(ChannelQueue queue, QueueStatus status) { switch (status) { case QueueStatus.Route: return(new RouteQueueState(queue)); case QueueStatus.Push: return(new PushQueueState(queue)); case QueueStatus.RoundRobin: return(new RoundRobinQueueState(queue)); case QueueStatus.Pull: return(new PullQueueState(queue)); case QueueStatus.Cache: return(new CacheQueueState(queue)); case QueueStatus.Paused: return(new PauseQueueState(queue)); case QueueStatus.Stopped: return(new StopQueueState(queue)); default: return(new StopQueueState(queue)); } }
public async Task FillData() { List <byte[]> items = new List <byte[]>(); for (int i = 0; i < 10; i++) { items.Add(Encoding.UTF8.GetBytes("No #" + i)); } TestMqServer server = new TestMqServer(); server.Initialize(40702); server.Start(300, 300); Channel channel = server.Server.FindChannel("ch-push"); Assert.NotNull(channel); ChannelQueue queue = channel.FindQueue(MessageA.ContentType); Assert.NotNull(queue); QueueFiller filler = new QueueFiller(queue); filler.FillData(items, false, true); filler.FillData(items, false, false); await Task.Delay(500); Assert.NotEmpty(queue.HighPriorityMessages); Assert.NotEmpty(queue.RegularMessages); }
public async Task FromClientToChannelManuel() { TestMqServer server = new TestMqServer(); server.Initialize(42305); server.Start(); TmqClient client = new TmqClient(); client.AutoAcknowledge = false; client.MessageReceived += async(c, m) => { await client.SendAsync(m.CreateAcknowledge()); }; await client.ConnectAsync("tmq://localhost:42305"); Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.RequestAcknowledge = true; //subscribe await client.Join(channel.Name, true); //push a message to the queue MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, true); Assert.True(sent); }
/// <summary> /// Handles pushing a message into a queue /// </summary> private async Task HandlePush(MqClient client, TmqMessage message, ChannelQueue queue) { //check authority if (_server.Authorization != null) { bool grant = await _server.Authorization.CanMessageToQueue(client, queue, message); if (!grant) { if (!string.IsNullOrEmpty(message.MessageId)) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized)); } return; } } //prepare the message QueueMessage queueMessage = new QueueMessage(message); queueMessage.Source = client; //push the message PushResult result = await queue.Push(queueMessage, client); if (result == PushResult.StatusNotSupported) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unacceptable)); } else if (result == PushResult.LimitExceeded) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.LimitExceeded)); } }
public async Task FromClientToChannelTimeout() { TestMqServer server = new TestMqServer(); server.Initialize(42306); server.Start(); TmqClient client = new TmqClient(); client.AutoAcknowledge = false; client.AcknowledgeTimeout = TimeSpan.FromSeconds(3); await client.ConnectAsync("tmq://localhost:42306"); Assert.True(client.IsConnected); Channel channel = server.Server.Channels.FirstOrDefault(); Assert.NotNull(channel); ChannelQueue queue = channel.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.RequestAcknowledge = true; //subscribe await client.Join(channel.Name, true); //push a message to the queue MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("Hello, World!")); bool sent = await client.Push(channel.Name, queue.Id, ms, true); Assert.False(sent); }
public async Task <bool> CanMessageToQueue(MqClient client, ChannelQueue queue, TmqMessage message) { bool grant = client.Type.Equals("producer"); Console.WriteLine("Can message to queue: " + grant); return(await Task.FromResult(grant)); }
public async Task RequestAcknowledge(bool queueAckIsActive) { int port = 47118 + Convert.ToInt32(queueAckIsActive); TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(300, 300); Channel ch = server.Server.FindChannel("ch-route"); ChannelQueue queue = ch.Queues.FirstOrDefault(); Assert.NotNull(queue); queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(3); queue.Options.RequestAcknowledge = queueAckIsActive; TmqClient producer = new TmqClient(); await producer.ConnectAsync("tmq://localhost:" + port); Assert.True(producer.IsConnected); TmqClient consumer = new TmqClient(); consumer.AutoAcknowledge = true; consumer.AcknowledgeTimeout = TimeSpan.FromSeconds(4); consumer.ClientId = "consumer"; await consumer.ConnectAsync("tmq://localhost:" + port); Assert.True(consumer.IsConnected); TmqResponseCode joined = await consumer.Join("ch-route", true); Assert.Equal(TmqResponseCode.Ok, joined); bool ack = await producer.Push("ch-route", MessageA.ContentType, "Hello, World!", true); Assert.Equal(queueAckIsActive, ack); }
private static async Task AddToQueue(ChannelQueue <PersonProper> channel, IList <PersonProper> people, CancellationToken token) { foreach (var person in people) { await channel.WriteAsync(person, cancellationToken : token).ConfigureAwait(false); } _ = channel.Lock(); }
/// <summary> /// Decision: Allow. /// If AcknowledgeWhen is AfterSent, acknowledge is sent to producer. /// </summary> public async Task <Decision> EndSend(ChannelQueue queue, QueueMessage message) { if (_when == AcknowledgeWhen.AfterSent) { return(await Task.FromResult(new Decision(true, false, false, DeliveryAcknowledgeDecision.Always))); } return(await Task.FromResult(new Decision(true, false))); }
public async Task WriteAsync02() { var channel = new ChannelQueue <PersonProper>(); var people = this.PersonProperList; await channel.WriteAsync(people).ConfigureAwait(false); this.Consumer.Consume(channel.Count); }
/// <summary> /// Decision: Allow. /// If AcknowledgeWhen is AfterReceived, acknowledge is sent to producer. /// </summary> public async Task <Decision> ReceivedFromProducer(ChannelQueue queue, QueueMessage message, MqClient sender) { if (_when == AcknowledgeWhen.AfterReceived) { return(await Task.FromResult(new Decision(true, false, false, DeliveryAcknowledgeDecision.Always))); } return(await Task.FromResult(new Decision(true, false))); }
public async Task WriteAsync02() { var channel = new ChannelQueue <PersonProper>(); var people = this.GetPersonProperArray(Tristate.False); await channel.WriteAsync(people).ConfigureAwait(false); base.Consumer.Consume(channel.Count); }
/// <summary> /// Decision: Allow. /// If AcknowledgeWhen is AfterAcknowledge, acknowledge is sent to producer. /// </summary> public async Task <Decision> AcknowledgeReceived(ChannelQueue queue, TmqMessage acknowledgeMessage, MessageDelivery delivery, bool success) { if (_when == AcknowledgeWhen.AfterAcknowledge) { return(await Task.FromResult(new Decision(true, false, false, DeliveryAcknowledgeDecision.Always))); } return(await Task.FromResult(new Decision(true, false))); }
public async Task WriteAsyncTest01() { var channel = new ChannelQueue <PersonProper>(); var person = RandomData.GenerateRefPerson <PersonProper>(); var token = CancellationToken.None; await channel.WriteAsync(person, cancellationToken : token).ConfigureAwait(false); Assert.IsTrue(channel.Count == 1); }
public async Task <Decision> ReceivedFromProducer(ChannelQueue queue, QueueMessage message, MqClient sender) { _server.OnReceived++; if (_server.SendAcknowledgeFromMQ) { return(await Task.FromResult(new Decision(true, false, false, DeliveryAcknowledgeDecision.Always))); } return(await Task.FromResult(new Decision(true, false))); }
/// <summary> /// Removes a queue from the channel /// </summary> public async Task RemoveQueue(ushort queueId) { ChannelQueue queue = FindQueue(queueId); if (queue == null) { return; } await RemoveQueue(queue); }
/// <summary> /// Removes a queue from a channel /// </summary> private async Task RemoveQueue(MqClient client, TmqMessage message) { if (_server.AdminAuthorization == null) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized)); } return; } Channel channel = _server.FindChannel(message.Target); if (channel == null) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound)); return; } byte[] bytes = new byte[2]; await message.Content.ReadAsync(bytes); ushort contentType = BitConverter.ToUInt16(bytes); ChannelQueue queue = channel.FindQueue(contentType); if (queue == null) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound)); return; } bool grant = await _server.AdminAuthorization.CanRemoveQueue(client, queue); if (!grant) { if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized)); } return; } await channel.RemoveQueue(queue); if (message.ResponseRequired) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Ok)); } }
/// <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 WriteAsync01() { var channel = new ChannelQueue <PersonProper>(); var people = this.PersonProperList; for (var peopleCount = 0; peopleCount < people.Count; peopleCount++) { await channel.WriteAsync(people[peopleCount]).ConfigureAwait(false); } this.Consumer.Consume(channel.Count); }
public async Task WriteAsync01() { var channel = new ChannelQueue <PersonProper>(); var people = this.GetPersonProperArray(Tristate.False); for (var peopleCount = 0; peopleCount < people.Length; peopleCount++) { await channel.WriteAsync(people[peopleCount]).ConfigureAwait(false); } base.Consumer.Consume(channel.Count); }
public async Task WriteAsyncTest03() { var channel = new ChannelQueue <PersonProper>(); const int Count = 100; var people = RandomData.GeneratePersonRefCollection <PersonProper>(Count); var token = CancellationToken.None; await channel.WriteAsync(people, lockQueue : true, cancellationToken : token).ConfigureAwait(false); Assert.IsTrue(channel.Count == Count); }
public async Task RequestAcknowledge() { int port = 47412; TestMqServer server = new TestMqServer(); server.Initialize(port); server.Start(300, 300); Channel channel = server.Server.FindChannel("ch-pull"); ChannelQueue queue = channel.FindQueue(MessageA.ContentType); Assert.NotNull(channel); Assert.NotNull(queue); queue.Options.RequestAcknowledge = true; queue.Options.AcknowledgeTimeout = TimeSpan.FromSeconds(15); TmqClient consumer = new TmqClient(); consumer.AutoAcknowledge = true; consumer.ClientId = "consumer"; await consumer.ConnectAsync("tmq://localhost:" + port); Assert.True(consumer.IsConnected); bool msgReceived = false; consumer.MessageReceived += (c, m) => msgReceived = true; TmqResponseCode joined = await consumer.Join("ch-pull", true); Assert.Equal(TmqResponseCode.Ok, joined); TmqClient producer = new TmqClient(); producer.AcknowledgeTimeout = TimeSpan.FromSeconds(15); await producer.ConnectAsync("tmq://localhost:" + port); Assert.True(producer.IsConnected); Task <bool> taskAck = producer.Push("ch-pull", MessageA.ContentType, "Hello, World!", true); await Task.Delay(500); Assert.False(taskAck.IsCompleted); Assert.False(msgReceived); Assert.Single(queue.RegularMessages); TmqMessage pull = await consumer.Pull("ch-pull", MessageA.ContentType); Assert.NotNull(pull); }
public async Task Handle(MqClient client, TmqMessage message) { //find channel and queue Channel channel = _server.FindChannel(message.Target); //if auto creation active, try to create channel if (channel == null && _server.Options.AutoChannelCreation) { channel = _server.FindOrCreateChannel(message.Target); } if (channel == null) { if (!string.IsNullOrEmpty(message.MessageId)) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound)); } return; } ChannelQueue queue = channel.FindQueue(message.ContentType); //if auto creation active, try to create queue if (queue == null && _server.Options.AutoQueueCreation) { queue = await channel.FindOrCreateQueue(message.ContentType); } if (queue == null) { if (!string.IsNullOrEmpty(message.MessageId)) { await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.NotFound)); } return; } //consumer is trying to pull from the queue //in false cases, we won't send any response, cuz client is pending only queue messages, not response messages if (message.Length == 0 && message.ResponseRequired) { await HandlePullRequest(client, message, channel, queue); } //message have a content, this is the real message from producer to the queue else { await HandlePush(client, message, queue); } }
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); }
public override void MoveOneStep(Valuation GlobalEnv, List <Configuration> list) { System.Diagnostics.Debug.Assert(list.Count == 0); Valuation globalEnv = GlobalEnv; ChannelQueue Buffer = null; if (globalEnv.Channels.TryGetValue(this.ChannelName, out Buffer)) { if (Buffer.Count < Buffer.Size) { ExpressionValue[] values = new ExpressionValue[ExpressionList.Length]; string eventName = ChannelName + "!"; string eventID = ChannelName + "!"; for (int i = 0; i < ExpressionList.Length; i++) { values[i] = EvaluatorDenotational.Evaluate(ExpressionList[i], globalEnv); if (i == ExpressionList.Length - 1) { eventName += values[i].ToString(); eventID += values[i].ExpressionID; } else { eventName += values[i].ToString() + "."; eventID += values[i].ExpressionID + "."; } } ChannelQueue newBuffer = Buffer.Clone(); newBuffer.Enqueue(values); globalEnv = globalEnv.GetChannelClone(); globalEnv.Channels[ChannelName] = newBuffer; if (eventID != eventName) { list.Add(new Configuration(Process, eventID, eventName, globalEnv, false)); } else { list.Add(new Configuration(Process, eventID, null, globalEnv, false)); } } } // return list; }
/// <summary> /// Return Valuation in Configuration of given BDD configuration in the column form /// [ REFS: '', DEREFS: ''] /// </summary> /// <param name="currentStateDD">current BDD configuration</param> /// <param name="initialValuation">based on Initial Valuation to get the global variables</param> /// <returns>Corresponding Valuation of the BDD configuration</returns> public Valuation GetValuationFromBDD(CUDDNode currentStateDD, Valuation initialValuation) { Valuation currentValuation = initialValuation.GetClone(); if (currentValuation.Variables != null && currentValuation.Variables.Count > 0) { foreach (StringDictionaryEntryWithKey<ExpressionValue> pair in currentValuation.Variables._entries) { if (pair != null) { if (pair.Value is RecordValue) { RecordValue array = pair.Value as RecordValue; ExpressionValue[] arrayValue = new ExpressionValue[array.Associations.Length]; for (int i = 0; i < array.Associations.Length; i++) { string variableName = pair.Key + Model.NAME_SEPERATOR + i.ToString(); int value = model.GetColVarValue(currentStateDD, variableName); arrayValue[i] = new IntConstant(value); } pair.Value = new RecordValue(arrayValue); } else if (pair.Value is BoolConstant) { string variableName = pair.Key; int value = model.GetColVarValue(currentStateDD, variableName); pair.Value = new BoolConstant(value == 1); } else { string variableName = pair.Key; int value = model.GetColVarValue(currentStateDD, variableName); pair.Value = new IntConstant(value); } } } } if (currentValuation.Channels != null && currentValuation.Channels.Count > 0) { List<string> channelNames = new List<string>(currentValuation.Channels.Keys); foreach (string channelName in channelNames) { int count = model.GetColVarValue(currentStateDD, Model.GetCountVarChannel(channelName)); int top = model.GetColVarValue(currentStateDD, Model.GetTopVarChannel(channelName)); ChannelQueue currentQueue = new ChannelQueue(count); int firstElement = 0; if (top >= count) { firstElement = top - count; } else { firstElement = top - count + model.mapChannelToSize[channelName]; } for (int i = 0; i < count; i++) { int elementSize = model.GetColVarValue(currentStateDD, Model.GetArrayOfSizeElementChannel(channelName) + Model.NAME_SEPERATOR + firstElement); ExpressionValue[] elementValues = new ExpressionValue[elementSize]; //Find values in the message for (int j = 0; j < elementSize; j++) { int subElementIndex = firstElement * Model.MAX_MESSAGE_LENGTH + j; int value = model.GetColVarValue(currentStateDD, channelName + Model.NAME_SEPERATOR + subElementIndex.ToString()); elementValues[j] = new IntConstant(value); } //Add element to queue currentQueue.Enqueue(elementValues); //update to the next element firstElement = (firstElement + 1) % model.mapChannelToSize[channelName]; } currentValuation.Channels[channelName] = currentQueue; } } return currentValuation; }