示例#1
0
        public void Roundtrip()
        {
            MockJob job = new MockJob()
            {
                Id = Guid.NewGuid(), Int32 = 987
            };
            JobDescriptor desc = factory.CreateDescriptor(job);
            JToken        token;

            Assert.That(desc, Is.Not.Null);
            Assert.That(desc.Job, Is.EqualTo("MockJob"));
            Assert.That(desc.Properties, Is.Not.Null.And.Count.EqualTo(2));

            Assert.That(desc.Properties.TryGetValue("Id", out token), Is.True);
            Assert.That(token, Is.TypeOf <JValue>().And.Property("Type").EqualTo(JTokenType.Guid));
            Assert.That(token.ToObject <Guid>(), Is.EqualTo(job.Id));

            Assert.That(desc.Properties.TryGetValue("Int32", out token), Is.True);
            Assert.That(token, Is.TypeOf <JValue>().And.Property("Type").EqualTo(JTokenType.Integer));
            Assert.That(token.ToObject <Int32>(), Is.EqualTo(987));

            Assert.That(desc.QueueMessageId, Is.Null);

            IJob job2 = factory.CreateJob(desc);

            Assert.That(job2, Is.Not.Null.And.TypeOf <MockJob>());
            MockJob mockJob2 = (MockJob)job2;

            Assert.That(mockJob2.Id, Is.EqualTo(job.Id));
        }
示例#2
0
        public void ExecuteFailed_Ask()
        {
            consumerSettings.FailedJobHandling         = FailedJobHandling.DecidePerJob;
            consumerSettings.FailedJobHandlingProvider = (job, descriptor, exception) =>
            {
                Assert.That(job, Is.InstanceOf <MockJob>());
                Assert.That(descriptor, Is.Not.Null);

                MockJob mockJob = (MockJob)job;

                switch (mockJob.Int32)
                {
                case 1:
                    Assert.That(exception, Is.Null);
                    return(FailedJobHandling.Delete);

                case 2:
                    Assert.That(exception, Is.Null);
                    return(FailedJobHandling.Requeue);

                case 3:
                    Assert.That(exception, Is.Null);
                    return((FailedJobHandling)999);

                default:
                    Assert.That(exception, Is.Null);
                    return(FailedJobHandling.DecidePerJob);
                }
            };

            MockJob.Callback = mockJob =>
            {
                return(false);
            };

            AddMessage("{\"Job\":\"MockJob\",\"Properties\":{\"Int32\":1}}");
            AddMessage("{\"Job\":\"MockJob\",\"Properties\":{\"Int32\":2}}");
            AddMessage("{\"Job\":\"MockJob\",\"Properties\":{\"Int32\":3}}");
            AddMessage("{\"Job\":\"MockJob\",\"Properties\":{\"Int32\":4}}");
            AddMessage("{\"Job\":\"MockJob\",\"Properties\":{\"Int32\":5}}");

            // Dequeue the first message -- it should be deleted.
            context = JobExecutionContext.Dequeue(this);

            Assert.That(context.Empty, Is.False);
            Assert.That(context.Execute(), Is.False);

            context.Dispose();

            // Dequeue the second message -- it should be requeued again.
            context = JobExecutionContext.Dequeue(this);

            Assert.That(context.Empty, Is.False);
            Assert.That(context.Execute(), Is.False);

            context.Dispose();

            // Dequeue the third message -- it should cause an exception because an unknown behavior is configured.
            context = JobExecutionContext.Dequeue(this);

            Assert.That(context.Empty, Is.False);
            Assert.Throws(Is.TypeOf <NotSupportedException>().And.Message.EqualTo("Unsupported FailedJobHandling: 999"),
                          () => context.Execute());

            context.Dispose();

            // Dequeue the fourth message -- it should cause an exception because the decision provider returned an unspported value.
            context = JobExecutionContext.Dequeue(this);

            Assert.That(context.Empty, Is.False);
            Assert.Throws(Is.TypeOf <NotSupportedException>().And
                          .Message.EqualTo("Unsupported FailedJobHandling: DecidePerJob"),
                          () => context.Execute());

            context.Dispose();

            // Dequeue the fifth message -- it should cause an exception because the decision provider is missing.
            consumerSettings.FailedJobHandlingProvider = null;
            context = JobExecutionContext.Dequeue(this);

            Assert.That(context.Empty, Is.False);
            Assert.Throws(Is.TypeOf <InvalidOperationException>().And
                          .Message.EqualTo("The FailedJobHandlingProvider must not be null when 'DecidePerJob' is used."),
                          () => context.Execute());

            context.Dispose();

            // Verify that the message NOT queued again.
            CloudQueueMessage message = queue.GetMessage();

            Assert.That(message, Is.Not.Null);
            Assert.That(message.AsString, Is.EqualTo("{\"Job\":\"MockJob\",\"Properties\":{\"Int32\":2}}"));
        }