Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a1cc93a43c3f6f7ceb1b5516dcfc0d77de701312 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2007, 2009, 2011, 2012, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.tests.cache;

import org.eclipse.net4j.util.ref.KeyedWeakReference;
import org.eclipse.net4j.util.tests.AbstractOMTest;

import java.lang.ref.ReferenceQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Eike Stepper
 */
public class SensitiveProtoTest extends AbstractOMTest
{
  @SuppressWarnings("unchecked")
  public void testSensitiveCacheWithEvictionPolicy() throws Exception
  {
    ConcurrentMap<Integer, KeyedAndValuedWeakReference<Integer, String>> map //
    = new ConcurrentHashMap<Integer, KeyedAndValuedWeakReference<Integer, String>>();

    ReferenceQueue<String> queue //
    = new ReferenceQueue<String>();

    for (int i = 0; i < 20; i++)
    {
      map.put(i, new KeyedAndValuedWeakReference<Integer, String>(i, String.valueOf(i), queue));
    }

    for (int gc = 0; gc < 10; gc++)
    {
      System.gc();
      sleep(100);
    }

    KeyedAndValuedWeakReference<Integer, String> ref;
    while ((ref = (KeyedAndValuedWeakReference<Integer, String>)queue.poll()) != null)
    {
      int i = ref.getKey();
      System.out.println("Dequeued i=" + i); //$NON-NLS-1$
      if (i < 10)
      {
        map.put(i, new KeyedAndValuedWeakReference<Integer, String>(i, ref.getValue(), queue));
      }
      else
      {
        map.remove(i, ref);
      }
    }

    assertEquals(10, map.size());
  }

  public static class KeyedAndValuedWeakReference<K, T> extends KeyedWeakReference<K, T>
  {
    private T value;

    public KeyedAndValuedWeakReference(K key, T ref, ReferenceQueue<T> queue)
    {
      super(key, ref, queue);
      value = ref;
    }

    public KeyedAndValuedWeakReference(K key, T ref)
    {
      super(key, ref);
      value = ref;
    }

    public T getValue()
    {
      return value;
    }
  }
}

Back to the top