Skip to content

Commit

Permalink
unit test (#97)
Browse files Browse the repository at this point in the history
unit test
  • Loading branch information
shihuili1218 committed May 28, 2024
1 parent b05738f commit 0b12640
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 16 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Codecov CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 18
uses: actions/setup-java@v1
with:
java-version: 18
- name: Install dependencies
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- name: Run tests and collect coverage
run: mvn -B test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ofcoder.klein.consensus.paxos;

import junit.framework.TestCase;

public class ProposalTest extends TestCase {

public void testGetGroup() {
Proposal proposal = new Proposal();
assertNull(proposal.getGroup());

proposal = new Proposal("group1", "data1");
assertEquals("group1", proposal.getGroup());

proposal.setGroup("group2");
assertEquals("group2", proposal.getGroup());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface CacheContainer {
* @param expire expire
* @return the previous value associated with key, or null
*/
Object put(String key, Object data, Long expire);
Object put(String key, Object data, long expire);

/**
* put element to cache if present, and set expire.
Expand All @@ -57,7 +57,7 @@ public interface CacheContainer {
* @param expire expire
* @return the previous value associated with key, or null
*/
Object putIfAbsent(String key, Object data, Long expire);
Object putIfAbsent(String key, Object data, long expire);

/**
* remove key from cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ protected int adjustTimeout(final int timeoutMs) {
}

@Override
public Object put(final String key, final Object data, final Long expire) {
public Object put(final String key, final Object data, final long expire) {
Object d = _put(key, data, expire);
waitClear(expire, key);
return d;
}

protected abstract Object _put(String key, Object data, Long expire);
protected abstract Object _put(String key, Object data, long expire);

@Override
public Object putIfAbsent(final String key, final Object data, final Long expire) {
public Object putIfAbsent(final String key, final Object data, final long expire) {
Object d = _putIfAbsent(key, data, expire);
if (d == null) {
waitClear(expire, key);
}
return d;
}

protected abstract Object _putIfAbsent(String key, Object data, Long expire);
protected abstract Object _putIfAbsent(String key, Object data, long expire);

private long roundToNextBucket(final long time) {
return (time / expirationInterval + 1) * expirationInterval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Object get(final String key) {
}

@Override
public synchronized Object _put(final String key, final Object data, final Long expire) {
public synchronized Object _put(final String key, final Object data, final long expire) {
MetaData value = new MetaData();
value.setExpire(expire);
value.setData(data);
Expand All @@ -101,7 +101,7 @@ public synchronized Object _put(final String key, final Object data, final Long
}

@Override
public synchronized Object _putIfAbsent(final String key, final Object data, final Long expire) {
public synchronized Object _putIfAbsent(final String key, final Object data, final long expire) {
MetaData value = new MetaData();
value.setExpire(expire);
value.setData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object get(final String key) {
}

@Override
public Object _put(final String key, final Object data, final Long expire) {
public Object _put(final String key, final Object data, final long expire) {
MetaData value = new MetaData();
value.setExpire(expire);
value.setData(data);
Expand All @@ -60,7 +60,7 @@ public Object _put(final String key, final Object data, final Long expire) {
}

@Override
public Object _putIfAbsent(final String key, final Object data, final Long expire) {
public Object _putIfAbsent(final String key, final Object data, final long expire) {
MetaData value = new MetaData();
value.setExpire(expire);
value.setData(data);
Expand Down
15 changes: 15 additions & 0 deletions klein-core/src/test/java/com/ofcoder/klein/KleinPropTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ofcoder.klein;

import org.junit.Assert;
import org.junit.Test;

public class KleinPropTest {

@Test
public void testGetStorage() {
KleinProp prop = new KleinProp();
Assert.assertEquals("file", prop.getStorage());
prop.setStorage("leveldb");
Assert.assertEquals("leveldb", prop.getStorage());
}
}
117 changes: 117 additions & 0 deletions klein-core/src/test/java/com/ofcoder/klein/core/cache/CacheSMTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.ofcoder.klein.core.cache;

import java.lang.reflect.Field;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CacheSMTest {

CacheSM cacheSM;

@Mock
private CacheContainer mockContainer;

@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
MockitoAnnotations.initMocks(this);
cacheSM = new CacheSM(new CacheProp()) {
@Override
public Object makeImage() {
return mockContainer;
}
};

Field containerField = CacheSM.class.getDeclaredField("container");
containerField.setAccessible(true);
containerField.set(cacheSM, mockContainer);
}

@Test
public void testApplyWithPutOperation() {
CacheMessage message = new CacheMessage();
message.setOp(CacheMessage.PUT);
message.setKey("key");
message.setData("data");
message.setExpire(1000L);

cacheSM.apply(message);
verify(mockContainer).put(eq("key"), eq("data"), eq(1000L));
}

@Test
public void testApplyWithGetOperation() {
CacheMessage message = new CacheMessage();
message.setOp(CacheMessage.GET);
message.setKey("key");

when(mockContainer.get("key")).thenReturn("data");

assertEquals("data", cacheSM.apply(message));
}

@Test
public void testApplyWithInvalidateOperation() {
CacheMessage message = new CacheMessage();
message.setOp(CacheMessage.INVALIDATE);
message.setKey("key");

cacheSM.apply(message);
verify(mockContainer).remove("key");
}

@Test
public void testApplyWithInvalidateAllOperation() {
CacheMessage message = new CacheMessage();
message.setOp(CacheMessage.INVALIDATEALL);

cacheSM.apply(message);
verify(mockContainer).clear();
}

@Test
public void testApplyWithPutIfPresentOperation() {
CacheMessage message = new CacheMessage();
message.setOp(CacheMessage.PUTIFPRESENT);
message.setKey("key");
message.setData("data");
message.setExpire(1000L);

when(mockContainer.putIfAbsent("key", "data", 1000L)).thenReturn(true);

assertEquals(true, cacheSM.apply(message));
}

@Test
public void testApplyWithExistOperation() {
CacheMessage message = new CacheMessage();
message.setOp(CacheMessage.EXIST);
message.setKey("key");

when(mockContainer.containsKey("key")).thenReturn(true);

assertEquals(true, cacheSM.apply(message));
}


@Test
public void testApplyWithUnknownMessage() {
assertNull(cacheSM.apply(new Object()));
verify(mockContainer, never()).put(any(), any(), anyLong());
verify(mockContainer, never()).get(any());
verify(mockContainer, never()).remove(any());
verify(mockContainer, never()).clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ofcoder.klein.core.cache;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

import org.junit.Before;
import org.junit.Test;

public class CacheSnapTest {

CacheSnap cacheSnap;

Object cache;

@Before
public void setUp() {
cache = mock(Object.class);
cacheSnap = new CacheSnap(cache, null);
}

@Test
public void testGetCache() {
assertEquals(cache, cacheSnap.getCache());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.ofcoder.klein.core.cache;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import junit.framework.TestCase;

public class MemoryCacheContainerTest extends TestCase {

MemoryCacheContainer cache;

public void test_MemoryMap_NoUse() {
int initialCapacity = 3;
LruCacheContainer.MemoryMap cache = new LruCacheContainer.MemoryMap(initialCapacity);
Expand Down Expand Up @@ -33,4 +37,61 @@ public void test_MemoryMap_Used() {
Assert.assertTrue(cache.containsKey("2"));
}

}
@Before
public void setUp() {
cache = new MemoryCacheContainer();
}

@Test
public void testContainsKey_ExistingKey_Expired() {
// Setup
String key = "key";
Long expire = 0L; // Expired
Object data = new Object();
cache._put(key, data, expire);

// Execute
boolean result = cache.containsKey(key);

// Verify
assertFalse(result);
}

@Test
public void testContainsKey_NonExistingKey() {
// Setup
String key = "key";

// Execute
boolean result = cache.containsKey(key);

// Verify
assertFalse(result);
}

@Test
public void testContainsKey_NullValue() {
// Setup
String key = "key";
cache._put(key, null, 1000L);

// Execute
boolean result = cache.containsKey(key);

// Verify
assertFalse(result);
}

@Test
public void testContainsKey_ExpiredAndNullValue() {
// Setup
String key = "key";
cache._put(key, null, 0L);

// Execute
boolean result = cache.containsKey(key);

// Verify
assertFalse(result);
}
}
Loading

0 comments on commit 0b12640

Please sign in to comment.