Search in sources :

Example 11 with Job

use of com.surftools.BeanstalkClient.Job in project camel by apache.

the class AwaitingConsumerTest method testReceive.

@Test
public void testReceive() throws Exception {
    if (!canTest()) {
        return;
    }
    final Job jobMock = mock(Job.class);
    final long jobId = 111;
    final byte[] payload = Helper.stringToBytes(testMessage);
    when(jobMock.getJobId()).thenReturn(jobId);
    when(jobMock.getData()).thenReturn(payload);
    when(client.reserve(anyInt())).thenReturn(jobMock).thenReturn(null);
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    result.expectedBodiesReceived(testMessage);
    result.expectedHeaderReceived(Headers.JOB_ID, jobId);
    result.message(0).header(Headers.JOB_ID).isEqualTo(jobId);
    result.assertIsSatisfied(100);
    verify(client, atLeast(1)).reserve(0);
    verify(client, atLeast(1)).delete(jobId);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Job(com.surftools.BeanstalkClient.Job) Test(org.junit.Test)

Example 12 with Job

use of com.surftools.BeanstalkClient.Job in project camel by apache.

the class ConsumerCompletionTest method testBeanstalkException.

@Test
public void testBeanstalkException() throws Exception {
    if (!canTest()) {
        return;
    }
    shouldIdie = false;
    final Job jobMock = mock(Job.class);
    final long jobId = 111;
    final byte[] payload = Helper.stringToBytes(testMessage);
    when(jobMock.getJobId()).thenReturn(jobId);
    when(jobMock.getData()).thenReturn(payload);
    when(client.reserve(anyInt())).thenThrow(new BeanstalkException("test")).thenReturn(jobMock);
    when(client.statsJob(anyInt())).thenReturn(null);
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    result.expectedBodiesReceived(testMessage);
    result.expectedHeaderReceived(Headers.JOB_ID, jobId);
    result.message(0).header(Headers.JOB_ID).isEqualTo(jobId);
    context.startRoute("foo");
    result.assertIsSatisfied();
    verify(client, atLeast(1)).reserve(anyInt());
    verify(client, times(1)).close();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Job(com.surftools.BeanstalkClient.Job) BeanstalkException(com.surftools.BeanstalkClient.BeanstalkException) Test(org.junit.Test)

Example 13 with Job

use of com.surftools.BeanstalkClient.Job in project camel by apache.

the class ConsumerCompletionTest method testReleaseOnFailure.

@Test
public void testReleaseOnFailure() throws Exception {
    shouldIdie = true;
    final long jobId = 111;
    final long priority = BeanstalkComponent.DEFAULT_PRIORITY;
    final int delay = BeanstalkComponent.DEFAULT_DELAY;
    final byte[] payload = Helper.stringToBytes(testMessage);
    final Job jobMock = mock(Job.class);
    when(jobMock.getJobId()).thenReturn(jobId);
    when(jobMock.getData()).thenReturn(payload);
    when(client.reserve(anyInt())).thenReturn(jobMock).thenReturn(null);
    when(client.statsJob(anyInt())).thenReturn(null);
    when(client.release(anyInt(), anyLong(), anyInt())).thenReturn(true);
    NotifyBuilder notify = new NotifyBuilder(context).whenFailed(1).create();
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(0);
    context.startRoute("foo");
    assertTrue(notify.matches(5, TimeUnit.SECONDS));
    verify(client, atLeastOnce()).reserve(anyInt());
    verify(client, atLeastOnce()).statsJob(anyInt());
    verify(client).release(jobId, priority, delay);
}
Also used : NotifyBuilder(org.apache.camel.builder.NotifyBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Job(com.surftools.BeanstalkClient.Job) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 14 with Job

use of com.surftools.BeanstalkClient.Job in project camel by apache.

the class ConsumerToProducerHeadersTest method testBeanstalkConsumerToProducer.

@Test
public void testBeanstalkConsumerToProducer() throws Exception {
    final long jobId = 111;
    final byte[] payload = Helper.stringToBytes(testMessage);
    final Job jobMock = mock(Job.class);
    // stats that may be set in the consumer:
    // mock stats : "tube", "state", "age", "time-left", "timeouts", "releases", "buries", "kicks"
    Map<String, String> stats = new HashMap<String, String>();
    stats.put("tube", "A");
    stats.put("state", "Test");
    stats.put("age", "0");
    stats.put("time-left", "0");
    stats.put("timeouts", "0");
    stats.put("releases", "0");
    stats.put("buries", "0");
    stats.put("kicks", "0");
    when(jobMock.getJobId()).thenReturn(jobId);
    when(jobMock.getData()).thenReturn(payload);
    when(client.reserve(anyInt())).thenReturn(jobMock).thenReturn(null);
    when(client.statsJob(anyInt())).thenReturn(stats);
    when(client.put(BeanstalkComponent.DEFAULT_PRIORITY, BeanstalkComponent.DEFAULT_DELAY, BeanstalkComponent.DEFAULT_TIME_TO_RUN, payload)).thenReturn(jobId);
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMinimumMessageCount(1);
    result.expectedBodiesReceived(testMessage);
    result.expectedHeaderReceived(Headers.JOB_ID, jobId);
    result.message(0).header(Headers.JOB_ID).isEqualTo(jobId);
    context.startRoute("foo");
    result.assertIsSatisfied();
    verify(client, atLeastOnce()).reserve(anyInt());
    verify(client, atLeastOnce()).statsJob(anyInt());
    assertEquals(((TestExchangeCopyProcessor) a).getExchangeCopy().getIn().getHeaders(), ((TestExchangeCopyProcessor) b).getExchangeCopy().getIn().getHeaders());
}
Also used : HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Job(com.surftools.BeanstalkClient.Job) Test(org.junit.Test)

Example 15 with Job

use of com.surftools.BeanstalkClient.Job in project camel by apache.

the class ImmediateConsumerTest method testDeleteOnSuccess.

@Test
public void testDeleteOnSuccess() throws Exception {
    if (!canTest()) {
        return;
    }
    final Job jobMock = mock(Job.class);
    final long jobId = 111;
    final byte[] payload = Helper.stringToBytes(testMessage);
    when(jobMock.getJobId()).thenReturn(jobId);
    when(jobMock.getData()).thenReturn(payload);
    when(client.reserve(anyInt())).thenReturn(jobMock).thenReturn(null);
    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedMessageCount(1);
    result.expectedBodiesReceived(testMessage);
    result.expectedHeaderReceived(Headers.JOB_ID, jobId);
    result.message(0).header(Headers.JOB_ID).isEqualTo(jobId);
    result.assertIsSatisfied(100);
    verify(client, atLeast(1)).reserve(0);
    verify(client, atLeast(1)).delete(jobId);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Job(com.surftools.BeanstalkClient.Job) Test(org.junit.Test)

Aggregations

Job (com.surftools.BeanstalkClient.Job)15 Test (org.junit.Test)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)9 Ignore (org.junit.Ignore)3 BeanstalkException (com.surftools.BeanstalkClient.BeanstalkException)2 HashMap (java.util.HashMap)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 Processor (org.apache.camel.Processor)1 NotifyBuilder (org.apache.camel.builder.NotifyBuilder)1