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 all 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 @@ -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) {
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,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);
}
}