Home | History | Annotate | Download | only in internal
      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 
     26 import java.util.Set;
     27 
     28 /**
     29  * Utilities for verifying com.google.inject.internal.WeakKeySet is not leaking memory.
     30  *
     31  * @author dweis (at) google.com (Daniel Weis)
     32  */
     33 public final class WeakKeySetUtils {
     34 
     35   private WeakKeySetUtils() {}
     36 
     37   public static void assertBlacklisted(Injector injector, Key<?> key) {
     38     assertBlacklistState(injector, key, true);
     39   }
     40 
     41   public static void assertNotBlacklisted(Injector injector, Key<?> key) {
     42     assertBlacklistState(injector, key, false);
     43   }
     44 
     45   public static void assertNotInSet(WeakKeySet set, Key<?> key) {
     46     // if we're expecting it to not be in the set, loop around and wait for threads to run.
     47     for (int i = 0; i < 10; i++) {
     48       if (!set.contains(key)) {
     49         break;
     50       }
     51       sleep();
     52     }
     53     assertFalse(set.contains(key));
     54     assertNull(set.getSources(Key.get(Integer.class)));
     55   }
     56 
     57   public static void assertInSet(WeakKeySet set, Key<?> key, int expectedSources,
     58       Object... sources) {
     59     assertTrue(set.contains(key));
     60     assertEquals(expectedSources, set.getSources(key).size());
     61     for (Object source : sources) {
     62       assertTrue("didn't contain source: " + source, set.getSources(key).contains(source));
     63     }
     64   }
     65 
     66   public static void assertSourceNotInSet(WeakKeySet set, Key<?> key, Object source) {
     67     // if we're expecting it to not be a source, loop around and wait for threads to run.
     68     for (int i = 0; i < 10; i++) {
     69       Set<Object> sources = set.getSources(key);
     70       assertNotNull("expected at least one source", source);
     71       if (!sources.contains(source)) {
     72         break;
     73       }
     74       sleep();
     75     }
     76     Set<Object> sources = set.getSources(key);
     77     assertNotNull("expected at least one source", source);
     78     assertFalse(sources.contains(source));
     79   }
     80 
     81   private static void assertBlacklistState(Injector injector, Key<?> key, boolean isBlacklisted) {
     82     // if we're expecting it to not be blacklisted, loop around and wait for threads to run.
     83     if (!isBlacklisted) {
     84       for (int i = 0; i < 10; i++) {
     85         if (!((InjectorImpl) injector).state.isBlacklisted(key)) {
     86           break;
     87         }
     88         sleep();
     89       }
     90     }
     91     assertEquals(isBlacklisted, ((InjectorImpl) injector).state.isBlacklisted(key));
     92   }
     93 
     94   private static void sleep() {
     95     try {
     96       Thread.sleep(1000);
     97     } catch (InterruptedException e) {
     98       throw new RuntimeException(e);
     99     }
    100     Thread.yield();
    101   }
    102 }
    103