Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTs for HazelcastTimerStore #4311

Merged
merged 6 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,11 @@
<artifactId>javaee-api</artifactId>
<version>${javaee.api.version}</version>
</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) {
pzygielo marked this conversation as resolved.
Show resolved Hide resolved
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,29 @@
package fish.payara.ejb.timer.hazelcast;

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

import org.junit.Test;

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

@Test
public void emptyTimersShallResultInZeroTimersCountedForServer() {
String [] serverIds = new String[] { "a" };

String [] counts = callListTimers(timers, serverIds);

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

@Test
public void emptyTimersShallResultInArrayOfTheSameSizeAsServerIds() {
String [] serverIds = new String[] { "a", "b", "c", "d" };

String [] counts = callListTimers(timers, serverIds);

assert counts.length == serverIds.length : "Size of counters array shall match the size of server ids array";
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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.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 [] serverIds = new String[] { "jb" };

String [] counts = callListTimers(timers, serverIds);

assert counts[0].equals("2");
pzygielo marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void countOneTimer() {
String [] serverIds = new String[] { "hz" };

String [] counts = callListTimers(timers, serverIds);

assert counts[0].equals("1");
}

@Test
public void noNullsExpectedInCountsForMissingTimers() {
String [] serverIds = new String[] { "jb", "ltd", "hz" };

String [] counts = callListTimers(timers, serverIds);

for (String count : counts) {
assert count != null : "Even for missing timers/server ids no null is expected but rather some representation of zero";
pzygielo marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Test
public void countersShallFollowServerIdOrder() {
String [] serverIds = new String[] { "hz", "ltd", "jb" };

String [] counts = callListTimers(timers, serverIds);

assert counts[0].equals("1");
assert counts[1].equals("0");
assert counts[2].equals("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) {
pzygielo marked this conversation as resolved.
Show resolved Hide resolved
return HazelcastTimerStore.listTimers(timers, serverIds);
}
}