Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 14 | package com.google.devtools.build.lib.supplier; |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
| 17 | |
| 18 | import com.google.common.testing.GcFinalization; |
| 19 | import java.lang.ref.WeakReference; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | import org.junit.runners.JUnit4; |
| 23 | |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 24 | /** Tests for {@link MemoizingInterruptibleSupplier}. */ |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 25 | @RunWith(JUnit4.class) |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 26 | public final class MemoizingInterruptibleSupplierTest { |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 27 | |
| 28 | private int callCount = 0; |
| 29 | private String returnVal = ""; |
| 30 | private CallCounter callCounter = new CallCounter(); |
| 31 | |
| 32 | private final class CallCounter { |
| 33 | public String call() { |
| 34 | ++callCount; |
| 35 | return returnVal; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | @Test |
Googler | 2c48ae8 | 2019-05-17 10:38:33 -0700 | [diff] [blame] | 40 | public void getReturnsCorrectResult() throws Exception { |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 41 | MemoizingInterruptibleSupplier<String> supplier = |
| 42 | MemoizingInterruptibleSupplier.of(callCounter::call); |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 43 | returnVal = "abc"; |
| 44 | |
| 45 | String result = supplier.get(); |
| 46 | |
| 47 | assertThat(result).isEqualTo("abc"); |
| 48 | } |
| 49 | |
| 50 | @Test |
Googler | 2c48ae8 | 2019-05-17 10:38:33 -0700 | [diff] [blame] | 51 | public void subsequentCallToGetReturnsCorrectResult() throws Exception { |
| 52 | MemoizingInterruptibleSupplier<String> supplier = |
| 53 | MemoizingInterruptibleSupplier.of(callCounter::call); |
| 54 | returnVal = "abc"; |
| 55 | |
| 56 | supplier.get(); |
| 57 | String result = supplier.get(); |
| 58 | |
| 59 | assertThat(result).isEqualTo("abc"); |
| 60 | } |
| 61 | |
| 62 | @Test |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 63 | public void onlyCallsDelegateOnce() throws Exception { |
| 64 | MemoizingInterruptibleSupplier<String> supplier = |
| 65 | MemoizingInterruptibleSupplier.of(callCounter::call); |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 66 | |
| 67 | supplier.get(); |
| 68 | supplier.get(); |
| 69 | |
| 70 | assertThat(callCount).isEqualTo(1); |
| 71 | } |
| 72 | |
| 73 | @Test |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 74 | public void freesReferenceToDelegeteAfterGet() throws Exception { |
| 75 | MemoizingInterruptibleSupplier<String> supplier = |
| 76 | MemoizingInterruptibleSupplier.of(callCounter::call); |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 77 | WeakReference<Object> ref = new WeakReference<>(callCounter); |
| 78 | callCounter = null; |
| 79 | |
| 80 | supplier.get(); |
| 81 | |
| 82 | GcFinalization.awaitClear(ref); |
| 83 | } |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 84 | |
| 85 | @Test |
Googler | 2c48ae8 | 2019-05-17 10:38:33 -0700 | [diff] [blame] | 86 | public void notInitializedBeforeCallingGet() { |
| 87 | MemoizingInterruptibleSupplier<String> supplier = |
| 88 | MemoizingInterruptibleSupplier.of(callCounter::call); |
| 89 | |
| 90 | boolean initialized = supplier.isInitialized(); |
| 91 | |
| 92 | assertThat(initialized).isFalse(); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void isInitializedAfterCallingGet() throws Exception { |
| 97 | MemoizingInterruptibleSupplier<String> supplier = |
| 98 | MemoizingInterruptibleSupplier.of(callCounter::call); |
| 99 | |
| 100 | supplier.get(); |
| 101 | boolean initialized = supplier.isInitialized(); |
| 102 | |
| 103 | assertThat(initialized).isTrue(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void isStillInitializedAfterSubsequentCallToGet() throws Exception { |
| 108 | MemoizingInterruptibleSupplier<String> supplier = |
| 109 | MemoizingInterruptibleSupplier.of(callCounter::call); |
| 110 | |
| 111 | supplier.get(); |
| 112 | supplier.get(); |
| 113 | boolean initialized = supplier.isInitialized(); |
| 114 | |
| 115 | assertThat(initialized).isTrue(); |
| 116 | } |
| 117 | |
| 118 | @Test |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 119 | public void of_returnsSameInstanceIfAlreadyMemoizing() { |
| 120 | InterruptibleSupplier<String> supplier = MemoizingInterruptibleSupplier.of(callCounter::call); |
| 121 | assertThat(MemoizingInterruptibleSupplier.of(supplier)).isSameInstanceAs(supplier); |
| 122 | } |
Googler | 24ed5b0 | 2019-02-15 10:16:17 -0800 | [diff] [blame] | 123 | } |