|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.activemq.artemis.core.protocol.mqtt; |
| 20 | + |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | +import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | +import java.util.concurrent.atomic.AtomicReference; |
| 27 | + |
| 28 | +import org.apache.activemq.artemis.core.config.Configuration; |
| 29 | +import org.apache.activemq.artemis.core.server.ActiveMQServer; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.Timeout; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | +import static org.junit.jupiter.api.Assertions.fail; |
| 36 | +import static org.mockito.Mockito.mock; |
| 37 | +import static org.mockito.Mockito.when; |
| 38 | + |
| 39 | +public class MQTTStateManagerTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + @Timeout(60) |
| 43 | + public void testGetSessionStateNeverReturnsNullUnderConcurrentRemoval() throws Exception { |
| 44 | + final ActiveMQServer server = mock(ActiveMQServer.class); |
| 45 | + final Configuration configuration = mock(Configuration.class); |
| 46 | + when(server.getConfiguration()).thenReturn(configuration); |
| 47 | + when(configuration.isMqttSubscriptionPersistenceEnabled()).thenReturn(false); |
| 48 | + |
| 49 | + final MQTTStateManager manager = MQTTStateManager.getInstance(server); |
| 50 | + try { |
| 51 | + final String clientId = "link-stealing-client"; |
| 52 | + final int iterations = 2_000_000; |
| 53 | + final int removerThreads = 3; |
| 54 | + |
| 55 | + final AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 56 | + final AtomicBoolean stop = new AtomicBoolean(false); |
| 57 | + final CountDownLatch start = new CountDownLatch(1); |
| 58 | + final ExecutorService pool = Executors.newFixedThreadPool(removerThreads + 1); |
| 59 | + |
| 60 | + for (int i = 0; i < removerThreads; i++) { |
| 61 | + pool.submit(() -> { |
| 62 | + awaitQuietly(start); |
| 63 | + while (!stop.get() && failure.get() == null) { |
| 64 | + try { |
| 65 | + manager.removeSessionState(clientId); |
| 66 | + } catch (Throwable t) { |
| 67 | + failure.compareAndSet(null, t); |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + pool.submit(() -> { |
| 75 | + awaitQuietly(start); |
| 76 | + try { |
| 77 | + for (int i = 0; i < iterations && failure.get() == null; i++) { |
| 78 | + MQTTSessionState state = manager.getSessionState(clientId); |
| 79 | + assertNotNull(state, "getSessionState(String) must never return null (ARTEMIS-6085)"); |
| 80 | + } |
| 81 | + } catch (Throwable t) { |
| 82 | + failure.compareAndSet(null, t); |
| 83 | + } finally { |
| 84 | + stop.set(true); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + start.countDown(); |
| 89 | + pool.shutdown(); |
| 90 | + assertTrue(pool.awaitTermination(50, TimeUnit.SECONDS), "concurrency test did not finish in time"); |
| 91 | + |
| 92 | + if (failure.get() != null) { |
| 93 | + fail("getSessionState() returned null / threw under concurrent removal (ARTEMIS-6085)", failure.get()); |
| 94 | + } |
| 95 | + } finally { |
| 96 | + MQTTStateManager.removeInstance(server); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static void awaitQuietly(CountDownLatch latch) { |
| 101 | + try { |
| 102 | + latch.await(); |
| 103 | + } catch (InterruptedException e) { |
| 104 | + Thread.currentThread().interrupt(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments