1 /* 2 * Copyright 2017, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.implcore.trace.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import io.opencensus.implcore.trace.internal.ConcurrentIntrusiveList.Element; 22 import javax.annotation.Nullable; 23 import org.junit.Rule; 24 import org.junit.Test; 25 import org.junit.rules.ExpectedException; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Unit tests for {@link ConcurrentIntrusiveList}. */ 30 @RunWith(JUnit4.class) 31 public class ConcurrentIntrusiveListTest { 32 private final ConcurrentIntrusiveList<FakeElement> intrusiveList = 33 new ConcurrentIntrusiveList<FakeElement>(); 34 @Rule public final ExpectedException exception = ExpectedException.none(); 35 36 @Test 37 public void emptyList() { 38 assertThat(intrusiveList.size()).isEqualTo(0); 39 assertThat(intrusiveList.getAll().isEmpty()).isTrue(); 40 } 41 42 @Test 43 public void addRemoveAdd_SameElement() { 44 FakeElement element = new FakeElement(); 45 intrusiveList.addElement(element); 46 assertThat(intrusiveList.size()).isEqualTo(1); 47 intrusiveList.removeElement(element); 48 assertThat(intrusiveList.size()).isEqualTo(0); 49 intrusiveList.addElement(element); 50 assertThat(intrusiveList.size()).isEqualTo(1); 51 } 52 53 @Test 54 public void addAndRemoveElements() { 55 FakeElement element1 = new FakeElement(); 56 FakeElement element2 = new FakeElement(); 57 FakeElement element3 = new FakeElement(); 58 intrusiveList.addElement(element1); 59 intrusiveList.addElement(element2); 60 intrusiveList.addElement(element3); 61 assertThat(intrusiveList.size()).isEqualTo(3); 62 assertThat(intrusiveList.getAll()).containsExactly(element3, element2, element1).inOrder(); 63 // Remove element from the middle of the list. 64 intrusiveList.removeElement(element2); 65 assertThat(intrusiveList.size()).isEqualTo(2); 66 assertThat(intrusiveList.getAll()).containsExactly(element3, element1).inOrder(); 67 // Remove element from the tail of the list. 68 intrusiveList.removeElement(element1); 69 assertThat(intrusiveList.size()).isEqualTo(1); 70 assertThat(intrusiveList.getAll().contains(element3)).isTrue(); 71 intrusiveList.addElement(element1); 72 assertThat(intrusiveList.size()).isEqualTo(2); 73 assertThat(intrusiveList.getAll()).containsExactly(element1, element3).inOrder(); 74 // Remove element from the head of the list when there are other elements after. 75 intrusiveList.removeElement(element1); 76 assertThat(intrusiveList.size()).isEqualTo(1); 77 assertThat(intrusiveList.getAll().contains(element3)).isTrue(); 78 // Remove element from the head of the list when no more other elements in the list. 79 intrusiveList.removeElement(element3); 80 assertThat(intrusiveList.size()).isEqualTo(0); 81 assertThat(intrusiveList.getAll().isEmpty()).isTrue(); 82 } 83 84 @Test 85 public void addAlreadyAddedElement() { 86 FakeElement element = new FakeElement(); 87 intrusiveList.addElement(element); 88 exception.expect(IllegalArgumentException.class); 89 intrusiveList.addElement(element); 90 } 91 92 @Test 93 public void removeNotAddedElement() { 94 FakeElement element = new FakeElement(); 95 exception.expect(IllegalArgumentException.class); 96 intrusiveList.removeElement(element); 97 } 98 99 private static final class FakeElement implements Element<FakeElement> { 100 @Nullable private FakeElement next = null; 101 @Nullable private FakeElement prev = null; 102 103 @Override 104 public FakeElement getNext() { 105 return next; 106 } 107 108 @Override 109 public void setNext(FakeElement element) { 110 next = element; 111 } 112 113 @Override 114 public FakeElement getPrev() { 115 return prev; 116 } 117 118 @Override 119 public void setPrev(FakeElement element) { 120 prev = element; 121 } 122 } 123 } 124