public void ShouldSendFailCommandOnExceptionInJobHandler()
        {
            // given
            var expectedRequest = new ActivateJobsRequest
            {
                Timeout           = 123_000L,
                MaxJobsToActivate = 1,
                Type   = "foo",
                Worker = "jobWorker"
            };

            var expectedFailRequest = new FailJobRequest
            {
                JobKey       = 1,
                ErrorMessage = "Job worker 'jobWorker' tried to handle job of type 'foo', but exception occured 'Fail'",
                Retries      = 2
            };

            TestService.AddRequestHandler(typeof(ActivateJobsRequest), request => CreateExpectedResponse());

            // when
            using (var jobWorker = ZeebeClient.NewWorker()
                                   .JobType("foo")
                                   .Handler((jobClient, job) =>
            {
                if (job.Key == 1)
                {
                    throw new Exception("Fail");
                }
            })
                                   .MaxJobsActive(1)
                                   .Name("jobWorker")
                                   .Timeout(TimeSpan.FromSeconds(123L))
                                   .PollInterval(TimeSpan.FromMilliseconds(100))
                                   .Open())
            {
                Assert.True(jobWorker.IsOpen());
                while (TestService.Requests.Count < 2)
                {
                }
            }

            // then
            var actualRequest = TestService.Requests[0];

            Assert.AreEqual(expectedRequest, actualRequest);

            var actualFailRequest = TestService.Requests[1];

            Assert.AreEqual(expectedFailRequest, actualFailRequest);
        }
예제 #2
0
        public void ShouldUseAutoCompleteWithWorker()
        {
            // given
            var expectedRequest = new ActivateJobsRequest
            {
                Timeout           = 5_000L,
                MaxJobsToActivate = 3,
                Type           = "foo",
                Worker         = "jobWorker",
                RequestTimeout = 5_000L
            };

            TestService.AddRequestHandler(
                typeof(ActivateJobsRequest),
                request => CreateExpectedResponse());

            // when
            using (var jobWorker = ZeebeClient.NewWorker()
                                   .JobType("foo")
                                   .Handler((jobClient, job) =>
            {
                Logger.Info("Handler has seen job '{0}'", job);
            })
                                   .AutoCompletion()
                                   .MaxJobsActive(3)
                                   .Name("jobWorker")
                                   .Timeout(TimeSpan.FromMilliseconds(5_000L))
                                   .PollInterval(TimeSpan.FromSeconds(5))
                                   .PollingTimeout(TimeSpan.FromMilliseconds(5_000L))
                                   .Open())
            {
                Assert.True(jobWorker.IsOpen());
                while (TestService.Requests[typeof(CompleteJobRequest)].Count < 3)
                {
                }
            }

            // then
            var actualRequest = TestService.Requests[typeof(ActivateJobsRequest)][0];

            Assert.AreEqual(expectedRequest, actualRequest);

            var completeJobRequests = TestService.Requests[typeof(CompleteJobRequest)].OfType <CompleteJobRequest>().Select(j => j.JobKey).ToList();

            Assert.AreEqual(3, completeJobRequests.Count);

            Assert.Contains(1, completeJobRequests);
            Assert.Contains(2, completeJobRequests);
            Assert.Contains(3, completeJobRequests);
        }
예제 #3
0
        public void ShouldSendRequestReceiveResponseAsExpected()
        {
            // given
            var expectedRequest = new ActivateJobsRequest
            {
                Timeout           = 123_000L,
                MaxJobsToActivate = 3,
                Type           = "foo",
                Worker         = "jobWorker",
                RequestTimeout = 5_000L
            };

            TestService.AddRequestHandler(typeof(ActivateJobsRequest), request => CreateExpectedResponse());

            // when
            var signal       = new EventWaitHandle(false, EventResetMode.AutoReset);
            var receivedJobs = new List <IJob>();

            using (var jobWorker = ZeebeClient.NewWorker()
                                   .JobType("foo")
                                   .Handler((jobClient, job) =>
            {
                receivedJobs.Add(job);
                if (receivedJobs.Count == 3)
                {
                    signal.Set();
                }
            })
                                   .MaxJobsActive(3)
                                   .Name("jobWorker")
                                   .Timeout(TimeSpan.FromSeconds(123L))
                                   .PollInterval(TimeSpan.FromMilliseconds(100))
                                   .PollingTimeout(TimeSpan.FromSeconds(5L))
                                   .Open())
            {
                Assert.True(jobWorker.IsOpen());
                signal.WaitOne();
            }

            // then
            var actualRequest = TestService.Requests[typeof(ActivateJobsRequest)][0];

            Assert.AreEqual(expectedRequest, actualRequest);

            Assert.AreEqual(receivedJobs.Count, 3);

            AssertJob(receivedJobs[0], 1);
            AssertJob(receivedJobs[1], 2);
            AssertJob(receivedJobs[2], 3);
        }
예제 #4
0
        public void ShouldFailWithZeroThreadCount()
        {
            // expected
            var aggregateException = Assert.Throws <ArgumentOutOfRangeException>(
                () =>
            {
                ZeebeClient.NewWorker()
                .JobType("foo")
                .Handler((jobClient, job) => { })
                .HandlerThreads(0);
            });

            StringAssert.Contains("Expected an handler thread count larger then zero, but got 0.", aggregateException.Message);
        }
예제 #5
0
        public void ShouldCompleteAfterSendFailCommandOnExceptionInJobHandler()
        {
            // given
            TestService.AddRequestHandler(typeof(ActivateJobsRequest), request => CreateExpectedResponse());

            // when
            using (var jobWorker = ZeebeClient.NewWorker()
                                   .JobType("foo")
                                   .Handler(async(jobClient, job) =>
            {
                if (job.Key == 2)
                {
                    throw new Exception("Fail");
                }

                await jobClient.NewCompleteJobCommand(job).Send();
            })
                                   .MaxJobsActive(3)
                                   .Name("jobWorker")
                                   .Timeout(TimeSpan.FromSeconds(123L))
                                   .PollInterval(TimeSpan.FromMilliseconds(100))
                                   .Open())
            {
                Assert.True(jobWorker.IsOpen());
                while (TestService.Requests[typeof(ActivateJobsRequest)].Count < 1 ||
                       TestService.Requests[typeof(FailJobRequest)].Count < 1 ||
                       TestService.Requests[typeof(CompleteJobRequest)].Count < 2)
                {
                }
            }

            // then
            var completeRequests = TestService.Requests[typeof(CompleteJobRequest)];

            Assert.AreEqual(1, ((CompleteJobRequest)completeRequests[0]).JobKey);
            Assert.AreEqual(3, ((CompleteJobRequest)completeRequests[1]).JobKey);
        }
예제 #6
0
        public void ShouldSendRequestsWithDifferentAmounts()
        {
            // given
            var expectedRequest = new ActivateJobsRequest
            {
                Timeout           = 123_000L,
                MaxJobsToActivate = 4,
                Type           = "foo",
                Worker         = "jobWorker",
                RequestTimeout = 5_000L
            };

            var expectedSecondRequest = new ActivateJobsRequest
            {
                Timeout           = 123_000L,
                MaxJobsToActivate = 2,
                Type           = "foo",
                Worker         = "jobWorker",
                RequestTimeout = 5_000L
            };

            TestService.AddRequestHandler(typeof(ActivateJobsRequest), request => CreateExpectedResponse());

            // when
            var receivedJobs = new List <IJob>();

            using (var jobWorker = ZeebeClient.NewWorker()
                                   .JobType("foo")
                                   .Handler((jobClient, job) =>
            {
                // block job handling
                receivedJobs.Add(job);
                if (receivedJobs.Count == 2)
                {
                    using (var signal = new EventWaitHandle(false, EventResetMode.AutoReset))
                    {
                        signal.WaitOne();
                    }
                }
            })
                                   .MaxJobsActive(4)
                                   .Name("jobWorker")
                                   .Timeout(TimeSpan.FromSeconds(123L))
                                   .PollInterval(TimeSpan.FromMilliseconds(100))
                                   .PollingTimeout(TimeSpan.FromMilliseconds(5_000L))
                                   .Open())
            {
                Assert.True(jobWorker.IsOpen());
                while (TestService.Requests[typeof(ActivateJobsRequest)].Count < 2)
                {
                }
            }

            // then
            var actualRequest = TestService.Requests[typeof(ActivateJobsRequest)][0];

            Assert.AreEqual(expectedRequest, actualRequest);

            var actualSecondRequest = TestService.Requests[typeof(ActivateJobsRequest)][1];

            Assert.AreEqual(expectedSecondRequest, actualSecondRequest);
        }