|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import static com.google.cloud.storage.TestUtils.assertAll; |
| 20 | +import static com.google.common.truth.Truth.assertThat; |
| 21 | + |
| 22 | +import com.google.cloud.storage.ObjectReadSessionImpl.ConcurrentIdentityMap; |
| 23 | +import com.google.common.base.MoreObjects; |
| 24 | +import com.google.common.util.concurrent.ListenableFuture; |
| 25 | +import com.google.common.util.concurrent.ListeningExecutorService; |
| 26 | +import com.google.common.util.concurrent.MoreExecutors; |
| 27 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.Executors; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import java.util.function.BiFunction; |
| 34 | +import org.junit.AfterClass; |
| 35 | +import org.junit.BeforeClass; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +public final class ObjectReadSessionTest { |
| 39 | + private static final AtomicInteger vCounter = new AtomicInteger(1); |
| 40 | + |
| 41 | + private static ListeningExecutorService exec; |
| 42 | + |
| 43 | + @BeforeClass |
| 44 | + public static void beforeClass() { |
| 45 | + exec = |
| 46 | + MoreExecutors.listeningDecorator( |
| 47 | + Executors.newFixedThreadPool(2, new ThreadFactoryBuilder().setDaemon(true).build())); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterClass |
| 51 | + public static void afterClass() { |
| 52 | + exec.shutdownNow(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void concurrentIdentityMap_basic() throws Exception { |
| 57 | + ConcurrentIdentityMap<Key, Value> map = new ConcurrentIdentityMap<>(); |
| 58 | + |
| 59 | + map.put(new Key("k1"), new Value()); |
| 60 | + map.put(new Key("k2"), new Value()); |
| 61 | + map.put(new Key("k3"), new Value()); |
| 62 | + map.put(new Key("k4"), new Value()); |
| 63 | + |
| 64 | + List<String> strings = map.drainEntries((k, v) -> String.format("%s -> %s", k, v)); |
| 65 | + assertThat(strings).hasSize(4); |
| 66 | + |
| 67 | + String joined = String.join("\n", strings); |
| 68 | + assertAll( |
| 69 | + () -> assertThat(joined).contains("k1"), |
| 70 | + () -> assertThat(joined).contains("k2"), |
| 71 | + () -> assertThat(joined).contains("k3"), |
| 72 | + () -> assertThat(joined).contains("k4")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void concurrentIdentityMap_multipleThreadsAdding() throws Exception { |
| 77 | + ConcurrentIdentityMap<Key, Value> map = new ConcurrentIdentityMap<>(); |
| 78 | + |
| 79 | + CountDownLatch cdl = new CountDownLatch(1); |
| 80 | + map.put(new Key("t1k1"), new Value()); |
| 81 | + map.put(new Key("t1k2"), new Value()); |
| 82 | + |
| 83 | + ListenableFuture<Boolean> submitted = |
| 84 | + exec.submit( |
| 85 | + () -> { |
| 86 | + try { |
| 87 | + boolean await = cdl.await(3, TimeUnit.SECONDS); |
| 88 | + assertThat(await).isTrue(); |
| 89 | + map.put(new Key("t2k1"), new Value()); |
| 90 | + return true; |
| 91 | + } catch (InterruptedException e) { |
| 92 | + throw new RuntimeException(e); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + BiFunction<Key, Value, String> f = |
| 97 | + (k, v) -> { |
| 98 | + cdl.countDown(); |
| 99 | + return String.format("%s -> %s", k, v); |
| 100 | + }; |
| 101 | + List<String> strings = map.drainEntries(f); |
| 102 | + assertThat(strings).hasSize(2); |
| 103 | + String joined = String.join("\n", strings); |
| 104 | + assertAll(() -> assertThat(joined).contains("t1k1"), () -> assertThat(joined).contains("t1k2")); |
| 105 | + |
| 106 | + submitted.get(1, TimeUnit.SECONDS); |
| 107 | + List<String> drain2 = map.drainEntries(f); |
| 108 | + assertThat(drain2).hasSize(1); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void concurrentIdentityMap_removeAfterDrainClean() throws Exception { |
| 113 | + ConcurrentIdentityMap<Key, Value> map = new ConcurrentIdentityMap<>(); |
| 114 | + |
| 115 | + CountDownLatch cdl = new CountDownLatch(1); |
| 116 | + map.put(new Key("t1k1"), new Value()); |
| 117 | + Key t1k2 = new Key("t1k2"); |
| 118 | + map.put(t1k2, new Value()); |
| 119 | + |
| 120 | + ListenableFuture<Boolean> submit = |
| 121 | + exec.submit( |
| 122 | + () -> { |
| 123 | + try { |
| 124 | + boolean await = cdl.await(3, TimeUnit.SECONDS); |
| 125 | + assertThat(await).isTrue(); |
| 126 | + map.remove(t1k2); |
| 127 | + return true; |
| 128 | + } catch (InterruptedException e) { |
| 129 | + throw new RuntimeException(e); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + BiFunction<Key, Value, String> f = |
| 134 | + (k, v) -> { |
| 135 | + cdl.countDown(); |
| 136 | + return String.format("%s -> %s", k, v); |
| 137 | + }; |
| 138 | + List<String> strings = map.drainEntries(f); |
| 139 | + assertThat(strings).hasSize(2); |
| 140 | + String joined = String.join("\n", strings); |
| 141 | + assertAll(() -> assertThat(joined).contains("t1k1"), () -> assertThat(joined).contains("t1k2")); |
| 142 | + |
| 143 | + assertThat(submit.get(1, TimeUnit.SECONDS)).isEqualTo(true); |
| 144 | + } |
| 145 | + |
| 146 | + private static final class Key { |
| 147 | + private final String k; |
| 148 | + |
| 149 | + private Key(String k) { |
| 150 | + this.k = k; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public String toString() { |
| 155 | + return MoreObjects.toStringHelper(this).add("k", k).toString(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static final class Value { |
| 160 | + private final String v = String.format("v/%d", vCounter.getAndIncrement()); |
| 161 | + |
| 162 | + @Override |
| 163 | + public String toString() { |
| 164 | + return MoreObjects.toStringHelper(this).add("v", v).toString(); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments