1 /* 2 * Copyright (C) 2014 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. 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 distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.google.inject.internal; 16 17 import static junit.framework.Assert.assertEquals; 18 import static junit.framework.Assert.assertFalse; 19 import static junit.framework.Assert.assertNotNull; 20 import static junit.framework.Assert.assertNull; 21 import static junit.framework.Assert.assertTrue; 22 23 import com.google.inject.Injector; 24 import com.google.inject.Key; 25 import java.util.Set; 26 27 /** 28 * Utilities for verifying com.google.inject.internal.WeakKeySet is not leaking memory. 29 * 30 * @author dweis@google.com (Daniel Weis) 31 */ 32 public final class WeakKeySetUtils { 33 WeakKeySetUtils()34 private WeakKeySetUtils() {} 35 assertBlacklisted(Injector injector, Key<?> key)36 public static void assertBlacklisted(Injector injector, Key<?> key) { 37 assertBlacklistState(injector, key, true); 38 } 39 assertNotBlacklisted(Injector injector, Key<?> key)40 public static void assertNotBlacklisted(Injector injector, Key<?> key) { 41 assertBlacklistState(injector, key, false); 42 } 43 assertNotInSet(WeakKeySet set, Key<?> key)44 public static void assertNotInSet(WeakKeySet set, Key<?> key) { 45 // if we're expecting it to not be in the set, loop around and wait for threads to run. 46 for (int i = 0; i < 10; i++) { 47 if (!set.contains(key)) { 48 break; 49 } 50 sleep(); 51 } 52 assertFalse(set.contains(key)); 53 assertNull(set.getSources(Key.get(Integer.class))); 54 } 55 assertInSet( WeakKeySet set, Key<?> key, int expectedSources, Object... sources)56 public static void assertInSet( 57 WeakKeySet set, Key<?> key, int expectedSources, Object... sources) { 58 assertTrue(set.contains(key)); 59 assertEquals(expectedSources, set.getSources(key).size()); 60 for (Object source : sources) { 61 assertTrue("didn't contain source: " + source, set.getSources(key).contains(source)); 62 } 63 } 64 assertSourceNotInSet(WeakKeySet set, Key<?> key, Object source)65 public static void assertSourceNotInSet(WeakKeySet set, Key<?> key, Object source) { 66 // if we're expecting it to not be a source, loop around and wait for threads to run. 67 for (int i = 0; i < 10; i++) { 68 Set<Object> sources = set.getSources(key); 69 assertNotNull("expected at least one source", source); 70 if (!sources.contains(source)) { 71 break; 72 } 73 sleep(); 74 } 75 Set<Object> sources = set.getSources(key); 76 assertNotNull("expected at least one source", source); 77 assertFalse(sources.contains(source)); 78 } 79 assertBlacklistState(Injector injector, Key<?> key, boolean isBlacklisted)80 private static void assertBlacklistState(Injector injector, Key<?> key, boolean isBlacklisted) { 81 // if we're expecting it to not be blacklisted, loop around and wait for threads to run. 82 if (!isBlacklisted) { 83 for (int i = 0; i < 10; i++) { 84 if (!((InjectorImpl) injector).state.isBlacklisted(key)) { 85 break; 86 } 87 sleep(); 88 } 89 } 90 assertEquals(isBlacklisted, ((InjectorImpl) injector).state.isBlacklisted(key)); 91 } 92 sleep()93 private static void sleep() { 94 try { 95 Thread.sleep(1000); 96 } catch (InterruptedException e) { 97 throw new RuntimeException(e); 98 } 99 Thread.yield(); 100 } 101 } 102