Skip to content

Commit

Permalink
Merge pull request #4311 from pzygielo/ut_for_store
Browse files Browse the repository at this point in the history
UTs for HazelcastTimerStore
  • Loading branch information
MarkWareham authored Dec 12, 2019
2 parents 8ea055f + 338b44f commit c290512
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,11 @@
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,13 @@ protected boolean isValidTimerForThisServer(TimerPrimaryKey timerId, RuntimeTime
return result;
}

@Override
public String[] listTimers(String[] serverIds) {
static String [] listTimers(Collection<HZTimer> timers, String[] serverIds) {
String result[] = new String[serverIds.length];

// count all server ids
HashMap<String, Long> counts = new HashMap<>();
for (Object timer : pkCache.values()) {
HZTimer hzTimer = (HZTimer) timer;
String serverName = hzTimer.getMemberName();
for (HZTimer timer : timers) {
String serverName = timer.getMemberName();
Long val = counts.get(serverName);
if (val == null) {
val = new Long(0);
Expand All @@ -499,6 +497,11 @@ public String[] listTimers(String[] serverIds) {
return result;
}

@Override
public String[] listTimers(String[] serverIds) {
return listTimers((Collection<HZTimer>)pkCache.values(), serverIds);
}

@Override
public int migrateTimers(String fromOwnerId) {
String ownerIdOfThisServer = getOwnerIdOfThisServer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fish.payara.ejb.timer.hazelcast;

import java.util.Collection;
import java.util.Collections;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class HazelcastTimerStoreEmptyTimersTest extends HazelcastTimerStoreTestBase {
private Collection<HZTimer> timers = Collections.emptyList();

@Test
public void emptyTimersShallResultInZeroTimersCountedForServer() {
String [] counts = callListTimers(timers, "a");

assertEquals("With no timers defined, zero timers is expected for given server id", "0", counts[0]);
}

@Test
public void emptyTimersShallResultInArrayOfTheSameSizeAsServerIds() {
String [] counts = callListTimers(timers, "a", "b", "c", "d");

assertEquals("Size of counters array shall match the size of server ids array", 4, counts.length);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package fish.payara.ejb.timer.hazelcast;

import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static java.util.Arrays.asList;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HazelcastTimerStoreTest extends HazelcastTimerStoreTestBase {
@Mock
private HZTimer timer1, timer2, timer3;

private Collection<HZTimer> timers;

@Before
public void setUpTimers() {
timers = asList(timer1, timer2, timer3);
when(timer1.getMemberName()).thenReturn("jb");
when(timer2.getMemberName()).thenReturn("hz");
when(timer3.getMemberName()).thenReturn("jb");
}


@Test
public void twoTimersForTheSameMemberNameShallBeCountedForTheSameServerId() {
String [] counts = callListTimers(timers, "jb");

assertEquals("2", counts[0]);
}

@Test
public void countOneTimer() {
String [] counts = callListTimers(timers, "hz");

assertEquals("1", counts[0]);
}

@Test
public void noNullsExpectedInCountsForMissingTimers() {
String [] counts = callListTimers(timers, "jb", "ltd", "hz");

for (String count : counts) {
assertNotNull("Even for missing timers/server ids no null is expected but rather some representation of zero", count);
}
}

@Test
public void countersShallFollowServerIdOrder() {
String [] counts = callListTimers(timers, "hz", "ltd", "jb");

assertEquals("1", counts[0]);
assertEquals("0", counts[1]);
assertEquals("2", counts[2]);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fish.payara.ejb.timer.hazelcast;

import java.util.Collection;

public abstract class HazelcastTimerStoreTestBase {
public String[] callListTimers(Collection<HZTimer> timers, String... serverIds) {
return HazelcastTimerStore.listTimers(timers, serverIds);
}
}

0 comments on commit c290512

Please sign in to comment.