Skip to content

Commit

Permalink
Added unit tests for RefCountedReleasable
Browse files Browse the repository at this point in the history
Signed-off-by: Kartik Ganesh <[email protected]>
  • Loading branch information
kartg committed Mar 8, 2022
1 parent ab5574e commit 280d2be
Showing 1 changed file with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.common.concurrent;

import org.junit.Before;
import org.opensearch.test.OpenSearchTestCase;

import java.util.concurrent.atomic.AtomicInteger;

public class RefCountedReleasableTests extends OpenSearchTestCase {

private AtomicInteger testRef;
private RefCountedReleasable<AtomicInteger> testObject;

@Before
public void setup() {
testRef = new AtomicInteger(0);
testObject = new RefCountedReleasable<>("test", testRef, testRef::incrementAndGet);
}

public void testInitialState() {
assertEquals("test", testObject.getName());
assertEquals(testRef, testObject.get());
assertEquals(testRef, testObject.get());
assertEquals(0, testObject.get().get());
assertEquals(1, testObject.refCount());
}

public void testIncRef() {
testObject.incRef();
assertEquals(2, testObject.refCount());
assertEquals(0, testObject.get().get());
}

public void testCloseWithoutInternal() {
testObject.incRef();
assertEquals(2, testObject.refCount());
testObject.close();
assertEquals(1, testObject.refCount());
assertEquals(0, testObject.get().get());
}

public void testCloseWithInternal() {
assertEquals(1, testObject.refCount());
testObject.close();
assertEquals(0, testObject.refCount());
assertEquals(1, testObject.get().get());
}

public void testIncRefAfterClose() {
assertEquals(1, testObject.refCount());
testObject.close();
assertEquals(0, testObject.refCount());
assertEquals(1, testObject.get().get());
assertThrows(IllegalStateException.class, () -> testObject.incRef());
}
}

0 comments on commit 280d2be

Please sign in to comment.