|
| 1 | +// Copyright (c) 2017-Present Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.perf; |
| 17 | + |
| 18 | +import com.rabbitmq.client.AMQP.BasicProperties; |
| 19 | +import com.rabbitmq.client.Channel; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | +import org.mockito.ArgumentCaptor; |
| 23 | +import org.mockito.Captor; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.MockitoAnnotations; |
| 26 | + |
| 27 | +import static java.util.Arrays.asList; |
| 28 | +import static org.hamcrest.Matchers.is; |
| 29 | +import static org.hamcrest.Matchers.nullValue; |
| 30 | +import static org.junit.Assert.assertThat; |
| 31 | +import static org.mockito.ArgumentMatchers.any; |
| 32 | +import static org.mockito.ArgumentMatchers.anyString; |
| 33 | +import static org.mockito.ArgumentMatchers.eq; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | + |
| 36 | +public class ProducerTest { |
| 37 | + |
| 38 | + @Mock |
| 39 | + Channel channel; |
| 40 | + |
| 41 | + @Captor |
| 42 | + private ArgumentCaptor<BasicProperties> propertiesCaptor; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void init() { |
| 46 | + MockitoAnnotations.initMocks(this); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void flagNone() throws Exception { |
| 51 | + flagProducer().run(); |
| 52 | + |
| 53 | + verify(channel).basicPublish(anyString(), anyString(), |
| 54 | + eq(false), eq(false), propertiesCaptor.capture(), |
| 55 | + any(byte[].class) |
| 56 | + ); |
| 57 | + |
| 58 | + assertThat(props().getDeliveryMode(), nullValue()); |
| 59 | + assertThat(props().getPriority(), nullValue()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void flagPersistent() throws Exception { |
| 64 | + flagProducer("persistent").run(); |
| 65 | + |
| 66 | + verify(channel).basicPublish(anyString(), anyString(), |
| 67 | + eq(false), eq(false), propertiesCaptor.capture(), |
| 68 | + any(byte[].class) |
| 69 | + ); |
| 70 | + |
| 71 | + assertThat(props().getDeliveryMode(), is(2)); |
| 72 | + assertThat(props().getPriority(), nullValue()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void flagMandatory() throws Exception { |
| 77 | + flagProducer("mandatory").run(); |
| 78 | + |
| 79 | + verify(channel).basicPublish(anyString(), anyString(), |
| 80 | + eq(true), eq(false), propertiesCaptor.capture(), |
| 81 | + any(byte[].class) |
| 82 | + ); |
| 83 | + |
| 84 | + assertThat(props().getDeliveryMode(), nullValue()); |
| 85 | + assertThat(props().getPriority(), nullValue()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void flagPriority() throws Exception { |
| 90 | + flagProducer("priority=10").run(); |
| 91 | + |
| 92 | + verify(channel).basicPublish(anyString(), anyString(), |
| 93 | + eq(false), eq(false), propertiesCaptor.capture(), |
| 94 | + any(byte[].class) |
| 95 | + ); |
| 96 | + |
| 97 | + assertThat(props().getDeliveryMode(), nullValue()); |
| 98 | + assertThat(props().getPriority(), is(10)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void flagPersistentMandatoryPriority() throws Exception { |
| 103 | + flagProducer("persistent", "mandatory", "priority=10").run(); |
| 104 | + |
| 105 | + verify(channel).basicPublish(anyString(), anyString(), |
| 106 | + eq(true), eq(false), propertiesCaptor.capture(), |
| 107 | + any(byte[].class) |
| 108 | + ); |
| 109 | + |
| 110 | + assertThat(props().getDeliveryMode(), is(2)); |
| 111 | + assertThat(props().getPriority(), is(10)); |
| 112 | + } |
| 113 | + |
| 114 | + Producer flagProducer(String... flags) { |
| 115 | + return new Producer( |
| 116 | + channel, "exchange", "id", false, |
| 117 | + asList(flags), |
| 118 | + 0, 0.0f, 1, |
| 119 | + -1, 30, |
| 120 | + new TimeSequenceMessageBodySource(new TimestampProvider(false, false), 1000), |
| 121 | + new TimestampProvider(false, false), |
| 122 | + stats(), |
| 123 | + new MulticastSet.CompletionHandler() { |
| 124 | + |
| 125 | + @Override |
| 126 | + public void waitForCompletion() { |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void countDown() { |
| 131 | + } |
| 132 | + } |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + BasicProperties props() { |
| 137 | + return propertiesCaptor.getValue(); |
| 138 | + } |
| 139 | + |
| 140 | + private Stats stats() { |
| 141 | + return new Stats(0) { |
| 142 | + |
| 143 | + @Override |
| 144 | + protected void report(long now) { |
| 145 | + |
| 146 | + } |
| 147 | + }; |
| 148 | + } |
| 149 | +} |
0 commit comments