|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.resolver.dns; |
| 17 | + |
| 18 | +import io.netty.channel.DefaultEventLoop; |
| 19 | +import io.netty.util.concurrent.ScheduledFuture; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.ValueSource; |
| 23 | + |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import static java.util.Arrays.asList; |
| 27 | +import static java.util.Collections.singletonList; |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +class CacheTest { |
| 31 | + |
| 32 | + @ParameterizedTest |
| 33 | + @ValueSource(strings = { "address", "negative" }) |
| 34 | + void testEvictWhenExpirationEventLoopTerminates(String value) { |
| 35 | + Cache<String> cache = newCache(); |
| 36 | + DefaultEventLoop originalLoop = new DefaultEventLoop(); |
| 37 | + DefaultEventLoop replacementLoop = new DefaultEventLoop(); |
| 38 | + try { |
| 39 | + cache.cache("netty.io", value, 86400, originalLoop); |
| 40 | + assertThat(cache.get("netty.io")).isEqualTo(singletonList(value)); |
| 41 | + |
| 42 | + originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 43 | + |
| 44 | + // A cancelled expiration must not leave entries cached indefinitely, even without further lookups. |
| 45 | + assertThat(cache.size()).isZero(); |
| 46 | + assertThat(cache.get("netty.io")).isNull(); |
| 47 | + |
| 48 | + cache.cache("netty.io", "fresh", 86400, replacementLoop); |
| 49 | + assertThat(cache.get("netty.io")).isEqualTo(singletonList("fresh")); |
| 50 | + } finally { |
| 51 | + originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 52 | + replacementLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 53 | + cache.clear(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testReplacingExpirationKeepsEntriesUntilNewEventLoopTerminates() { |
| 59 | + Cache<String> cache = newCache(); |
| 60 | + DefaultEventLoop originalLoop = new DefaultEventLoop(); |
| 61 | + DefaultEventLoop replacementLoop = new DefaultEventLoop(); |
| 62 | + try { |
| 63 | + cache.cache("netty.io", "first", 86400, originalLoop); |
| 64 | + cache.cache("netty.io", "second", 3600, replacementLoop); |
| 65 | + originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 66 | + |
| 67 | + // Cancelling the superseded timer must not evict entries now owned by the shorter timer. |
| 68 | + assertThat(cache.get("netty.io")).isEqualTo(asList("first", "second")); |
| 69 | + |
| 70 | + replacementLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 71 | + assertThat(cache.size()).isZero(); |
| 72 | + assertThat(cache.get("netty.io")).isNull(); |
| 73 | + } finally { |
| 74 | + originalLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 75 | + replacementLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 76 | + cache.clear(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testExpirationCancelledBeforeScheduleReturns() { |
| 82 | + Cache<String> cache = newCache(); |
| 83 | + DefaultEventLoop loop = new DefaultEventLoop() { |
| 84 | + @Override |
| 85 | + public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { |
| 86 | + ScheduledFuture<?> future = super.schedule(command, delay, unit); |
| 87 | + submit(new Runnable() { |
| 88 | + @Override |
| 89 | + public void run() { |
| 90 | + // NOOP |
| 91 | + } |
| 92 | + }).syncUninterruptibly(); |
| 93 | + shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 94 | + return future; |
| 95 | + } |
| 96 | + }; |
| 97 | + try { |
| 98 | + cache.cache("netty.io", "address", 86400, loop); |
| 99 | + assertThat(cache.size()).isZero(); |
| 100 | + assertThat(cache.get("netty.io")).isNull(); |
| 101 | + } finally { |
| 102 | + loop.shutdownGracefully(0, 0, TimeUnit.SECONDS).syncUninterruptibly(); |
| 103 | + cache.clear(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static Cache<String> newCache() { |
| 108 | + return new Cache<String>() { |
| 109 | + @Override |
| 110 | + protected boolean shouldReplaceAll(String entry) { |
| 111 | + return "negative".equals(entry); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected boolean equals(String entry, String otherEntry) { |
| 116 | + return entry.equals(otherEntry); |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | +} |