Skip to content

Commit

Permalink
Initial work for testing module life cycle - TRUNK-4134
Browse files Browse the repository at this point in the history
  • Loading branch information
dkayiwa committed Nov 7, 2013
1 parent 9902022 commit b83b4ec
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 0 deletions.
101 changes: 101 additions & 0 deletions api/src/test/java/org/openmrs/module/ModuleActivatorTest.java
@@ -0,0 +1,101 @@
package org.openmrs.module;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.SkipBaseSetup;
import org.openmrs.test.StartModule;

@SkipBaseSetup
@StartModule({ "org/openmrs/module/include/test3-1.0-SNAPSHOT.omod", "org/openmrs/module/include/test1-1.0-SNAPSHOT.omod",
"org/openmrs/module/include/test2-1.0-SNAPSHOT.omod" })
public class ModuleActivatorTest extends BaseContextSensitiveTest {

private static final String MODULE1_ID = "test1";

private static final String MODULE2_ID = "test2";

private static final String MODULE3_ID = "test3";

ModuleTestData moduleTestData;

@Before
public void beforeEachTest() {
moduleTestData = ModuleTestData.getInstance();
}

@Test
public void shouldCallWillStartOnStartup() throws Exception {
assertTrue(moduleTestData.getWillStartCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getWillStartCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getWillStartCallCount(MODULE3_ID) == 1);
}

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 7, 2013

Member

I also you are missing tests for when you start a module that is previously stopped

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 7, 2013

Author Member

Yes i should add those too. I just made a quick commit for some review in case am taking a wrong direction. :)

@Test
@Ignore("This is work in progress. So not yet finished this")
public void shouldCallWillRefreshContextOnStartup() throws Exception {
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE3_ID) == 1);
}

@Test
@Ignore("This is work in progress. So not yet looked into why this fails")
public void shouldCallStartedOnStartup() throws Exception {
assertTrue(moduleTestData.getStartedCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getStartedCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getStartedCallCount(MODULE3_ID) == 1);
}

@Test
public void shouldStartModulesInOrder() throws Exception {
//module test2 depends on test1
//while test3 depends on test2
assertTrue(moduleTestData.getWillStartCallTime(MODULE1_ID) <= moduleTestData.getWillStartCallTime(MODULE2_ID));

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 13, 2013

Member

The module engine doesn't use start up order currently to call willStart so this is not actually testing the ordering at all, just directly call Modulefactory.getStartedModuelsInOrder and check the order

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 14, 2013

Author Member

So does this mean our current code can call willStart() for module2 before module1? (Module2 depends on Module1)

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 14, 2013

Member

willStart gets called in the correct order since the modules get started in the correct order always but the code that calls it doesn't reference Modulefactory.getStartedModuelsInOrder(), so you need separate tests that explicitly check that willRefreshContext(), contextRefreshed() and started() get called in the expected order

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 14, 2013

Author Member

So i should add some new tests ensuring that willRefreshContext, refreshContext and started get called in the expected order?

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 14, 2013

Member

Yes, how you can prove this is try to change the logic in Modulefactory.getStartedModuelsInOrder() to do no ordering and just do as below:

List modules = new ArrayList();
modules.addAll(getStartedModules());
return modules;

You will notice that this tests still always passes but the others to test willRefreshContext(), contextRefreshed() and started() when you add them they won't always pass

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 14, 2013

Author Member

OK

assertTrue(moduleTestData.getWillStartCallTime(MODULE2_ID) <= moduleTestData.getWillStartCallTime(MODULE3_ID));
}

@Test
public void shouldCallWillStopAndStoppedOnlyForStoppedModule() throws Exception {
ModuleFactory.stopModule(ModuleFactory.getModuleById(MODULE3_ID));

//should have called willStop() for only module test3
assertTrue(moduleTestData.getWillStopCallCount(MODULE3_ID) == 1);
assertTrue(moduleTestData.getWillStopCallCount(MODULE1_ID) == 0);
assertTrue(moduleTestData.getWillStopCallCount(MODULE2_ID) == 0);

//should have called stopped() for only module test3
assertTrue(moduleTestData.getStoppedCallCount(MODULE3_ID) == 1);
assertTrue(moduleTestData.getStoppedCallCount(MODULE1_ID) == 0);
assertTrue(moduleTestData.getStoppedCallCount(MODULE2_ID) == 0);

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 7, 2013

Member

it should also have called willRefreshContext and contextRefreshed for the remaining modules

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 11, 2013

Author Member

Fair point. Adding that shortly!

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 14, 2013

Member

You forgot to check that willRefreshContext() and contextRefreshed() for the remaining modules
Also you it would nice to check that ModuleFactory.getStartedModules doesn't contain the stopped module

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 14, 2013

Member

Same for the test for shouldStopDependantModulesOnStopModule()

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 14, 2013

Author Member

Sure, let me do so!

}

@Test
public void shouldCallWillStopAndStoppedOnShutdown() throws Exception {
ModuleUtil.shutdown();

//should have called willStop()
assertTrue(moduleTestData.getWillStopCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getWillStopCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getWillStopCallCount(MODULE3_ID) == 1);

//should have called stopped()
assertTrue(moduleTestData.getStoppedCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getStoppedCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getStoppedCallCount(MODULE3_ID) == 1);

//willStop() should have been called before stopped()
assertTrue(moduleTestData.getWillStopCallTime(MODULE1_ID) <= moduleTestData.getStoppedCallTime(MODULE1_ID));
assertTrue(moduleTestData.getWillStopCallTime(MODULE2_ID) <= moduleTestData.getStoppedCallTime(MODULE2_ID));
assertTrue(moduleTestData.getWillStopCallTime(MODULE3_ID) <= moduleTestData.getStoppedCallTime(MODULE3_ID));
}

private void init() {
moduleTestData.init(MODULE1_ID);
moduleTestData.init(MODULE2_ID);
moduleTestData.init(MODULE3_ID);
}
}
196 changes: 196 additions & 0 deletions api/src/test/java/org/openmrs/module/ModuleTestData.java
@@ -0,0 +1,196 @@
package org.openmrs.module;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ModuleTestData {

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 7, 2013

Member

I'm a little confused by the relationship between ModuleTestData and ModuleTestDataHolder, so how do call the methods from the activator methods? Or even how do they access the instance variable you are creating from the beforeEachTest method

This comment has been minimized.

Copy link
@dkayiwa

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 7, 2013

Member

I get the constructor/initialization, it means you use the same ModuleTestData instance for all the tests in the test class which might not be what you want since it would mean the counts would get incremented across different test methods and also it makes the code in beforeEachTest() method redundant because it executes moduleTest = ModuleTestData.getInstance() which will always be returning the same instance.

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 7, 2013

Author Member

Oh yeah ModuleTestData.getInstance() in beforeEachTest is redundant.
As for the counts being incremented in different test methods, did you see the followup commit where i call init() in beforeEachTest()?

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 7, 2013

Member

Ok i have seen it


private Map<String, Integer> willRefreshContextCallCount = new HashMap<String, Integer>();

private Map<String, Integer> contextRefreshedCallCount = new HashMap<String, Integer>();

private Map<String, Integer> willStartCallCount = new HashMap<String, Integer>();

private Map<String, Integer> startedCallCount = new HashMap<String, Integer>();

private Map<String, Integer> willStopCallCount = new HashMap<String, Integer>();

private Map<String, Integer> stoppedCallCount = new HashMap<String, Integer>();

private Map<String, Long> willRefreshContextCallTime = new HashMap<String, Long>();

private Map<String, Long> contextRefreshedCallTime = new HashMap<String, Long>();

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 7, 2013

Member

Are you using **callTime to determine that they were called in the expected order?

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 7, 2013

Author Member

Yes.


private Map<String, Long> willStartCallTime = new HashMap<String, Long>();

private Map<String, Long> startedCallTime = new HashMap<String, Long>();

private Map<String, Long> willStopCallTime = new HashMap<String, Long>();

private Map<String, Long> stoppedCallTime = new HashMap<String, Long>();

private ModuleTestData() {

}

private static class ModuleTestDataHolder {

private static ModuleTestData INSTANCE = null;
}

public static ModuleTestData getInstance() {
if (ModuleTestDataHolder.INSTANCE == null)
ModuleTestDataHolder.INSTANCE = new ModuleTestData();

return ModuleTestDataHolder.INSTANCE;
}

public void init(String moduleId) {
willRefreshContextCallCount.put(moduleId, 0);
contextRefreshedCallCount.put(moduleId, 0);
willStartCallCount.put(moduleId, 0);
startedCallCount.put(moduleId, 0);
willStopCallCount.put(moduleId, 0);
stoppedCallCount.put(moduleId, 0);

willRefreshContextCallTime.put(moduleId, 0l);
contextRefreshedCallTime.put(moduleId, 0l);
willStartCallTime.put(moduleId, 0l);
startedCallTime.put(moduleId, 0l);
willStopCallTime.put(moduleId, 0l);
stoppedCallTime.put(moduleId, 0l);
}

public Integer getWillRefreshContextCallCount(String moduleId) {
Integer count = willRefreshContextCallCount.get(moduleId);
if (count == null) {
count = 0;
}
return count;
}

public Integer getContextRefreshedCallCount(String moduleId) {
Integer count = contextRefreshedCallCount.get(moduleId);
if (count == null) {
count = 0;
}
return count;
}

public Integer getWillStartCallCount(String moduleId) {
Integer count = willStartCallCount.get(moduleId);
if (count == null) {
count = 0;
}
return count;
}

public Integer getStartedCallCount(String moduleId) {
Integer count = startedCallCount.get(moduleId);
if (count == null) {
count = 0;
}
return count;
}

public Integer getWillStopCallCount(String moduleId) {
Integer count = willStopCallCount.get(moduleId);
if (count == null) {
count = 0;
}
return count;
}

public Integer getStoppedCallCount(String moduleId) {
Integer count = stoppedCallCount.get(moduleId);
if (count == null) {
count = 0;
}
return count;
}

public void willRefreshContext(String moduleId) {
willRefreshContextCallTime.put(moduleId, new Date().getTime());

Integer count = willRefreshContextCallCount.get(moduleId);
if (count == null) {
count = 0;
}
willRefreshContextCallCount.put(moduleId, count + 1);
}

public void contextRefreshed(String moduleId) {
contextRefreshedCallTime.put(moduleId, new Date().getTime());

Integer count = contextRefreshedCallCount.get(moduleId);
if (count == null) {
count = 0;
}
contextRefreshedCallCount.put(moduleId, count + 1);
}

public void willStart(String moduleId) {
willStartCallTime.put(moduleId, new Date().getTime());

Integer count = willStartCallCount.get(moduleId);
if (count == null) {
count = 0;
}
willStartCallCount.put(moduleId, count + 1);
}

public void started(String moduleId) {
startedCallTime.put(moduleId, new Date().getTime());

Integer count = startedCallCount.get(moduleId);
if (count == null) {
count = 0;
}
startedCallCount.put(moduleId, count + 1);
}

public void willStop(String moduleId) {
willStopCallTime.put(moduleId, new Date().getTime());

Integer count = willStopCallCount.get(moduleId);
if (count == null) {
count = 0;
}
willStopCallCount.put(moduleId, count + 1);
}

public void stopped(String moduleId) {
stoppedCallTime.put(moduleId, new Date().getTime());

Integer count = stoppedCallCount.get(moduleId);
if (count == null) {
count = 0;
}
stoppedCallCount.put(moduleId, count + 1);
}

public Long getWillRefreshContextCallTime(String moduleId) {
return willRefreshContextCallTime.get(moduleId);
}

public Long getContextRefreshedCallTime(String moduleId) {
return contextRefreshedCallTime.get(moduleId);
}

public Long getWillStartCallTime(String moduleId) {
return willStartCallTime.get(moduleId);
}

public Long getStartedCallTime(String moduleId) {
return startedCallTime.get(moduleId);
}

public Long getWillStopCallTime(String moduleId) {
return willStopCallTime.get(moduleId);
}

public Long getStoppedCallTime(String moduleId) {
return stoppedCallTime.get(moduleId);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit b83b4ec

Please sign in to comment.