blob: 5408e728a65542ca4b7cd366f52c362ef3820f00 [file] [log] [blame]
Googler24ed5b02019-02-15 10:16:17 -08001// 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.
Googler407d3932019-05-16 15:13:59 -070014package com.google.devtools.build.lib.supplier;
Googler24ed5b02019-02-15 10:16:17 -080015
16import static com.google.common.truth.Truth.assertThat;
17
18import com.google.common.testing.GcFinalization;
19import java.lang.ref.WeakReference;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.junit.runners.JUnit4;
23
Googler407d3932019-05-16 15:13:59 -070024/** Tests for {@link MemoizingInterruptibleSupplier}. */
Googler24ed5b02019-02-15 10:16:17 -080025@RunWith(JUnit4.class)
Googler407d3932019-05-16 15:13:59 -070026public final class MemoizingInterruptibleSupplierTest {
Googler24ed5b02019-02-15 10:16:17 -080027
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
Googler2c48ae82019-05-17 10:38:33 -070040 public void getReturnsCorrectResult() throws Exception {
Googler407d3932019-05-16 15:13:59 -070041 MemoizingInterruptibleSupplier<String> supplier =
42 MemoizingInterruptibleSupplier.of(callCounter::call);
Googler24ed5b02019-02-15 10:16:17 -080043 returnVal = "abc";
44
45 String result = supplier.get();
46
47 assertThat(result).isEqualTo("abc");
48 }
49
50 @Test
Googler2c48ae82019-05-17 10:38:33 -070051 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
Googler407d3932019-05-16 15:13:59 -070063 public void onlyCallsDelegateOnce() throws Exception {
64 MemoizingInterruptibleSupplier<String> supplier =
65 MemoizingInterruptibleSupplier.of(callCounter::call);
Googler24ed5b02019-02-15 10:16:17 -080066
67 supplier.get();
68 supplier.get();
69
70 assertThat(callCount).isEqualTo(1);
71 }
72
73 @Test
Googler407d3932019-05-16 15:13:59 -070074 public void freesReferenceToDelegeteAfterGet() throws Exception {
75 MemoizingInterruptibleSupplier<String> supplier =
76 MemoizingInterruptibleSupplier.of(callCounter::call);
Googler24ed5b02019-02-15 10:16:17 -080077 WeakReference<Object> ref = new WeakReference<>(callCounter);
78 callCounter = null;
79
80 supplier.get();
81
82 GcFinalization.awaitClear(ref);
83 }
Googler407d3932019-05-16 15:13:59 -070084
85 @Test
Googler2c48ae82019-05-17 10:38:33 -070086 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
Googler407d3932019-05-16 15:13:59 -0700119 public void of_returnsSameInstanceIfAlreadyMemoizing() {
120 InterruptibleSupplier<String> supplier = MemoizingInterruptibleSupplier.of(callCounter::call);
121 assertThat(MemoizingInterruptibleSupplier.of(supplier)).isSameInstanceAs(supplier);
122 }
Googler24ed5b02019-02-15 10:16:17 -0800123}