aboutsummaryrefslogtreecommitdiff
path: root/impl_core/src/test/java/io/opencensus/implcore/trace/internal/ConcurrentIntrusiveListTest.java
blob: b41adb5e491e5661342a1705d09c83c5b43a15fa (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2017, Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.opencensus.implcore.trace.internal;

import static com.google.common.truth.Truth.assertThat;

import io.opencensus.implcore.trace.internal.ConcurrentIntrusiveList;
import io.opencensus.implcore.trace.internal.ConcurrentIntrusiveList.Element;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link ConcurrentIntrusiveList}. */
@RunWith(JUnit4.class)
public class ConcurrentIntrusiveListTest {
  private final ConcurrentIntrusiveList<FakeElement> intrusiveList =
      new ConcurrentIntrusiveList<FakeElement>();
  @Rule public final ExpectedException exception = ExpectedException.none();

  @Test
  public void emptyList() {
    assertThat(intrusiveList.size()).isEqualTo(0);
    assertThat(intrusiveList.getAll().isEmpty()).isTrue();
  }

  @Test
  public void addRemoveAdd_SameElement() {
    FakeElement element = new FakeElement();
    intrusiveList.addElement(element);
    assertThat(intrusiveList.size()).isEqualTo(1);
    intrusiveList.removeElement(element);
    assertThat(intrusiveList.size()).isEqualTo(0);
    intrusiveList.addElement(element);
    assertThat(intrusiveList.size()).isEqualTo(1);
  }

  @Test
  public void addAndRemoveElements() {
    FakeElement element1 = new FakeElement();
    FakeElement element2 = new FakeElement();
    FakeElement element3 = new FakeElement();
    intrusiveList.addElement(element1);
    intrusiveList.addElement(element2);
    intrusiveList.addElement(element3);
    assertThat(intrusiveList.size()).isEqualTo(3);
    assertThat(intrusiveList.getAll()).containsExactly(element3, element2, element1).inOrder();
    // Remove element from the middle of the list.
    intrusiveList.removeElement(element2);
    assertThat(intrusiveList.size()).isEqualTo(2);
    assertThat(intrusiveList.getAll()).containsExactly(element3, element1).inOrder();
    // Remove element from the tail of the list.
    intrusiveList.removeElement(element1);
    assertThat(intrusiveList.size()).isEqualTo(1);
    assertThat(intrusiveList.getAll().contains(element3)).isTrue();
    intrusiveList.addElement(element1);
    assertThat(intrusiveList.size()).isEqualTo(2);
    assertThat(intrusiveList.getAll()).containsExactly(element1, element3).inOrder();
    // Remove element from the head of the list when there are other elements after.
    intrusiveList.removeElement(element1);
    assertThat(intrusiveList.size()).isEqualTo(1);
    assertThat(intrusiveList.getAll().contains(element3)).isTrue();
    // Remove element from the head of the list when no more other elements in the list.
    intrusiveList.removeElement(element3);
    assertThat(intrusiveList.size()).isEqualTo(0);
    assertThat(intrusiveList.getAll().isEmpty()).isTrue();
  }

  @Test
  public void addAlreadyAddedElement() {
    FakeElement element = new FakeElement();
    intrusiveList.addElement(element);
    exception.expect(IllegalArgumentException.class);
    intrusiveList.addElement(element);
  }

  @Test
  public void removeNotAddedElement() {
    FakeElement element = new FakeElement();
    exception.expect(IllegalArgumentException.class);
    intrusiveList.removeElement(element);
  }

  private static final class FakeElement implements Element<FakeElement> {
    private FakeElement next = null;
    private FakeElement prev = null;

    @Override
    public FakeElement getNext() {
      return next;
    }

    @Override
    public void setNext(FakeElement element) {
      next = element;
    }

    @Override
    public FakeElement getPrev() {
      return prev;
    }

    @Override
    public void setPrev(FakeElement element) {
      prev = element;
    }
  }
}