killbill-memoizeit
Changes
beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java 5(+1 -4)
entitlement/src/test/java/com/ning/billing/entitlement/EntitlementTestListenerStatus.java 59(+0 -59)
entitlement/src/test/java/com/ning/billing/entitlement/EntitlementTestSuiteWithEmbeddedDB.java 17(+4 -13)
entitlement/src/test/java/com/ning/billing/entitlement/glue/TestEntitlementModuleWithEmbeddedDB.java 3(+0 -3)
junction/src/test/java/com/ning/billing/junction/glue/TestJunctionModuleWithEmbeddedDB.java 3(+0 -3)
subscription/src/test/java/com/ning/billing/subscription/api/user/TestSubscriptionHelper.java 11(+2 -9)
subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiChangePlan.java 36(+7 -29)
subscription/src/test/java/com/ning/billing/subscription/DefaultSubscriptionTestInitializer.java 7(+2 -5)
subscription/src/test/java/com/ning/billing/subscription/glue/TestDefaultSubscriptionModule.java 3(+0 -3)
subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestListenerStatus.java 59(+0 -59)
Details
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/BeatrixIntegrationModule.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/BeatrixIntegrationModule.java
index 257b917..32f9374 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/BeatrixIntegrationModule.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/BeatrixIntegrationModule.java
@@ -26,6 +26,7 @@ import com.ning.billing.DBTestingHelper;
import com.ning.billing.GuicyKillbillTestWithEmbeddedDBModule;
import com.ning.billing.account.api.AccountService;
import com.ning.billing.account.glue.DefaultAccountModule;
+import com.ning.billing.api.TestApiListener;
import com.ning.billing.beatrix.DefaultBeatrixService;
import com.ning.billing.beatrix.glue.BeatrixModule;
import com.ning.billing.beatrix.integration.overdue.IntegrationTestOverdueModule;
@@ -141,6 +142,8 @@ public class BeatrixIntegrationModule extends AbstractModule {
bind(PaymentChecker.class).asEagerSingleton();
bind(RefundChecker.class).asEagerSingleton();
bind(AuditChecker.class).asEagerSingleton();
+
+ bind(TestApiListener.class).asEagerSingleton();
}
private static final class DefaultInvoiceModuleWithSwitchRepairLogic extends DefaultInvoiceModule {
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java
index 1f12d13..fa22e11 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java
@@ -27,8 +27,6 @@ import com.ning.billing.api.TestApiListener.NextEvent;
import com.ning.billing.beatrix.osgi.SetupBundleWithAssertion;
import com.ning.billing.util.tag.Tag;
-import static org.testng.Assert.assertTrue;
-
public class TestJrubyNotificationPlugin extends TestOSGIBase {
private final String BUNDLE_TEST_RESOURCE_PREFIX = "killbill-notification-test";
@@ -55,11 +53,10 @@ public class TestJrubyNotificationPlugin extends TestOSGIBase {
// We wait for all that to occur and declare victory if we see the TagDefinition/Tag creation.
busHandler.pushExpectedEvents(NextEvent.TAG_DEFINITION, NextEvent.TAG);
final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(4));
- assertTrue(busHandler.isCompleted(2 * DELAY));
+ assertListenerStatus();
final List<Tag> tags = tagUserApi.getTagsForAccount(account.getId(), false, callContext);
Assert.assertEquals(tags.size(), 1);
//final Tag tag = tags.get(0);
}
-
}
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java
index 9d1ce91..05eecd2 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegration.java
@@ -501,7 +501,6 @@ public class TestIntegration extends TestIntegrationBase {
//
// VERIFY CTD HAS BEEN SET
//
- busHandler.reset();
DefaultSubscriptionBase subscription = (DefaultSubscriptionBase) baseEntitlement.getSubscriptionBase();
final DateTime startDate = subscription.getCurrentPhaseStart();
final BigDecimal rate = subscription.getCurrentPhase().getFixedPrice().getPrice(Currency.USD);
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
index c6d30c9..2b845da 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestIntegrationBase.java
@@ -44,7 +44,6 @@ import com.ning.billing.account.api.AccountService;
import com.ning.billing.account.api.AccountUserApi;
import com.ning.billing.api.TestApiListener;
import com.ning.billing.api.TestApiListener.NextEvent;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.beatrix.BeatrixTestSuiteWithEmbeddedDB;
import com.ning.billing.beatrix.glue.BeatrixModule;
import com.ning.billing.beatrix.lifecycle.Lifecycle;
@@ -104,15 +103,13 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
-public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implements TestListenerStatus {
+public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB {
protected static final DateTimeZone testTimeZone = DateTimeZone.UTC;
protected static final Logger log = LoggerFactory.getLogger(TestIntegrationBase.class);
protected static long AT_LEAST_ONE_MONTH_MS = 32L * 24L * 3600L * 1000L;
- protected static final long DELAY = 10000;
-
@Inject
protected Lifecycle lifecycle;
@@ -202,36 +199,17 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
@Inject
protected CacheControllerDispatcher controlCacheDispatcher;
+ @Inject
protected TestApiListener busHandler;
- private boolean isListenerFailed;
- private String listenerFailedMsg;
-
- @Override
- public void failed(final String msg) {
- isListenerFailed = true;
- listenerFailedMsg = msg;
- }
-
- @Override
- public void resetTestListenerStatus() {
- isListenerFailed = false;
- listenerFailedMsg = null;
- }
-
protected void assertListenerStatus() {
- assertTrue(busHandler.isCompleted(DELAY));
- if (isListenerFailed) {
- log.error(listenerFailedMsg);
- Assert.fail(listenerFailedMsg);
- }
+ busHandler.assertListenerStatus();
}
@BeforeClass(groups = "slow")
public void beforeClass() throws Exception {
final Injector g = Guice.createInjector(Stage.PRODUCTION, new BeatrixIntegrationModule(configSource));
g.injectMembers(this);
- busHandler = new TestApiListener(this, idbi);
SetupBundleWithAssertion setupTest = new SetupBundleWithAssertion("whatever", osgiConfig, "whatever");
setupTest.cleanBundleInstallDir();
@@ -249,7 +227,6 @@ public class TestIntegrationBase extends BeatrixTestSuiteWithEmbeddedDB implemen
controlCacheDispatcher.clearAll();
clock.resetDeltaFromReality();
- resetTestListenerStatus();
busHandler.reset();
// Start services
diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java
index 7e62cb7..36dcc45 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/TestPublicBus.java
@@ -64,7 +64,6 @@ public class TestPublicBus extends TestIntegrationBase {
log.debug("RESET TEST FRAMEWORK");
clock.resetDeltaFromReality();
- resetTestListenerStatus();
busHandler.reset();
// Start services
diff --git a/entitlement/src/test/java/com/ning/billing/entitlement/EntitlementTestSuiteWithEmbeddedDB.java b/entitlement/src/test/java/com/ning/billing/entitlement/EntitlementTestSuiteWithEmbeddedDB.java
index e0396c1..da2e112 100644
--- a/entitlement/src/test/java/com/ning/billing/entitlement/EntitlementTestSuiteWithEmbeddedDB.java
+++ b/entitlement/src/test/java/com/ning/billing/entitlement/EntitlementTestSuiteWithEmbeddedDB.java
@@ -33,7 +33,6 @@ import com.ning.billing.account.api.AccountData;
import com.ning.billing.account.api.AccountInternalApi;
import com.ning.billing.account.api.AccountUserApi;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.bus.api.PersistentBus;
import com.ning.billing.catalog.DefaultCatalogService;
import com.ning.billing.catalog.api.Catalog;
@@ -69,9 +68,6 @@ public class EntitlementTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWi
protected static final Logger log = LoggerFactory.getLogger(EntitlementTestSuiteWithEmbeddedDB.class);
- // Be generous...
- protected static final Long DELAY = 20000L;
-
@Inject
protected AccountUserApi accountApi;
@Inject
@@ -97,8 +93,6 @@ public class EntitlementTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWi
@Inject
protected TestApiListener testListener;
@Inject
- protected TestListenerStatus testListenerStatus;
- @Inject
protected BusService busService;
@Inject
protected SubscriptionBaseService subscriptionBaseService;
@@ -132,7 +126,7 @@ public class EntitlementTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWi
@BeforeMethod(groups = "slow")
public void beforeMethod() throws Exception {
super.beforeMethod();
- startTestFamework(testListener, testListenerStatus, clock, busService, subscriptionBaseService, entitlementService);
+ startTestFamework(testListener, clock, busService, subscriptionBaseService, entitlementService);
this.catalog = initCatalog(catalogService);
// Make sure we start with a clean state
@@ -157,14 +151,13 @@ public class EntitlementTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWi
private void startTestFamework(final TestApiListener testListener,
- final TestListenerStatus testListenerStatus,
final ClockMock clock,
final BusService busService,
final SubscriptionBaseService subscriptionBaseService,
final EntitlementService entitlementService) throws Exception {
log.debug("STARTING TEST FRAMEWORK");
- resetTestListener(testListener, testListenerStatus);
+ resetTestListener(testListener);
resetClockToStartOfTest(clock);
@@ -188,11 +181,10 @@ public class EntitlementTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWi
log.debug("STOPPED TEST FRAMEWORK");
}
- private void resetTestListener(final TestApiListener testListener, final TestListenerStatus testListenerStatus) {
+ private void resetTestListener(final TestApiListener testListener) {
// RESET LIST OF EXPECTED EVENTS
if (testListener != null) {
testListener.reset();
- testListenerStatus.resetTestListenerStatus();
}
}
@@ -250,7 +242,6 @@ public class EntitlementTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWi
}
protected void assertListenerStatus() {
- assertTrue(testListener.isCompleted(DELAY));
- ((EntitlementTestListenerStatus) testListenerStatus).assertListenerStatus();
+ testListener.assertListenerStatus();
}
}
diff --git a/entitlement/src/test/java/com/ning/billing/entitlement/glue/TestEntitlementModuleWithEmbeddedDB.java b/entitlement/src/test/java/com/ning/billing/entitlement/glue/TestEntitlementModuleWithEmbeddedDB.java
index 0b40737..9945069 100644
--- a/entitlement/src/test/java/com/ning/billing/entitlement/glue/TestEntitlementModuleWithEmbeddedDB.java
+++ b/entitlement/src/test/java/com/ning/billing/entitlement/glue/TestEntitlementModuleWithEmbeddedDB.java
@@ -21,9 +21,7 @@ import org.skife.config.ConfigSource;
import com.ning.billing.GuicyKillbillTestWithEmbeddedDBModule;
import com.ning.billing.account.glue.DefaultAccountModule;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.catalog.glue.CatalogModule;
-import com.ning.billing.entitlement.EntitlementTestListenerStatus;
import com.ning.billing.subscription.glue.DefaultSubscriptionModule;
import com.ning.billing.util.glue.AuditModule;
import com.ning.billing.util.glue.BusModule;
@@ -52,7 +50,6 @@ public class TestEntitlementModuleWithEmbeddedDB extends TestEntitlementModule {
install(new DefaultSubscriptionModule(configSource));
install(new AuditModule());
- bind(TestListenerStatus.class).to(EntitlementTestListenerStatus.class).asEagerSingleton();
bind(TestApiListener.class).asEagerSingleton();
}
}
diff --git a/junction/src/test/java/com/ning/billing/junction/glue/TestJunctionModuleWithEmbeddedDB.java b/junction/src/test/java/com/ning/billing/junction/glue/TestJunctionModuleWithEmbeddedDB.java
index 32dff2d..aa477d3 100644
--- a/junction/src/test/java/com/ning/billing/junction/glue/TestJunctionModuleWithEmbeddedDB.java
+++ b/junction/src/test/java/com/ning/billing/junction/glue/TestJunctionModuleWithEmbeddedDB.java
@@ -21,10 +21,8 @@ import org.skife.config.ConfigSource;
import com.ning.billing.GuicyKillbillTestWithEmbeddedDBModule;
import com.ning.billing.account.glue.DefaultAccountModule;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.catalog.glue.CatalogModule;
import com.ning.billing.entitlement.glue.DefaultEntitlementModule;
-import com.ning.billing.junction.JunctionTestListenerStatus;
import com.ning.billing.subscription.glue.DefaultSubscriptionModule;
import com.ning.billing.util.glue.BusModule;
import com.ning.billing.util.glue.MetricsModule;
@@ -53,7 +51,6 @@ public class TestJunctionModuleWithEmbeddedDB extends TestJunctionModule {
install(new MetricsModule());
install(new TagStoreModule());
- bind(TestListenerStatus.class).to(JunctionTestListenerStatus.class).asEagerSingleton();
bind(TestApiListener.class).asEagerSingleton();
}
}
diff --git a/junction/src/test/java/com/ning/billing/junction/JunctionTestSuiteWithEmbeddedDB.java b/junction/src/test/java/com/ning/billing/junction/JunctionTestSuiteWithEmbeddedDB.java
index 20750d0..baec0b4 100644
--- a/junction/src/test/java/com/ning/billing/junction/JunctionTestSuiteWithEmbeddedDB.java
+++ b/junction/src/test/java/com/ning/billing/junction/JunctionTestSuiteWithEmbeddedDB.java
@@ -32,7 +32,6 @@ import com.ning.billing.GuicyKillbillTestSuiteWithEmbeddedDB;
import com.ning.billing.account.api.AccountData;
import com.ning.billing.account.api.AccountUserApi;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.bus.api.PersistentBus;
import com.ning.billing.catalog.DefaultCatalogService;
import com.ning.billing.catalog.api.Catalog;
@@ -62,9 +61,6 @@ public abstract class JunctionTestSuiteWithEmbeddedDB extends GuicyKillbillTestS
protected static final Logger log = LoggerFactory.getLogger(JunctionTestSuiteWithEmbeddedDB.class);
- // Be generous...
- protected static final Long DELAY = 20000L;
-
@Inject
protected AccountUserApi accountApi;
@Inject
@@ -82,8 +78,6 @@ public abstract class JunctionTestSuiteWithEmbeddedDB extends GuicyKillbillTestS
@Inject
protected TestApiListener testListener;
@Inject
- protected TestListenerStatus testListenerStatus;
- @Inject
protected BusService busService;
@Inject
protected SubscriptionBaseService subscriptionBaseService;
@@ -136,7 +130,7 @@ public abstract class JunctionTestSuiteWithEmbeddedDB extends GuicyKillbillTestS
private void startTestFamework() throws Exception {
log.debug("STARTING TEST FRAMEWORK");
- resetTestListener(testListener, testListenerStatus);
+ resetTestListener(testListener);
resetClockToStartOfTest(clock);
@@ -156,11 +150,10 @@ public abstract class JunctionTestSuiteWithEmbeddedDB extends GuicyKillbillTestS
log.debug("STOPPED TEST FRAMEWORK");
}
- private void resetTestListener(final TestApiListener testListener, final TestListenerStatus testListenerStatus) {
+ private void resetTestListener(final TestApiListener testListener) {
// RESET LIST OF EXPECTED EVENTS
if (testListener != null) {
testListener.reset();
- testListenerStatus.resetTestListenerStatus();
}
}
@@ -218,7 +211,6 @@ public abstract class JunctionTestSuiteWithEmbeddedDB extends GuicyKillbillTestS
}
protected void assertListenerStatus() {
- assertTrue(testListener.isCompleted(DELAY));
- ((JunctionTestListenerStatus) testListenerStatus).assertListenerStatus();
+ testListener.assertListenerStatus();
}
}
diff --git a/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java b/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java
index 00e18f0..eae6a0f 100644
--- a/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java
+++ b/overdue/src/main/java/com/ning/billing/overdue/applicator/OverdueStateApplicator.java
@@ -151,14 +151,16 @@ public class OverdueStateApplicator {
return;
}
- storeNewState(account, nextOverdueState, context);
-
cancelSubscriptionsIfRequired(account, nextOverdueState, context);
sendEmailIfRequired(billingState, account, nextOverdueState, context);
avoid_extra_credit_by_toggling_AUTO_INVOICE_OFF(account, previousOverdueState, nextOverdueState, context);
+ // Make sure to store the new state last here: the entitlement DAO will send a BlockingTransitionInternalEvent
+ // on the bus to which invoice will react. We need the latest state (including AUTO_INVOICE_OFF tag for example)
+ // to be present in the database first.
+ storeNewState(account, nextOverdueState, context);
} catch (OverdueApiException e) {
if (e.getCode() != ErrorCode.OVERDUE_NO_REEVALUATION_INTERVAL.getCode()) {
throw new OverdueException(e);
diff --git a/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java b/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java
index c65501a..e314986 100644
--- a/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java
+++ b/server/src/test/java/com/ning/billing/jaxrs/TestJaxrsBase.java
@@ -111,11 +111,13 @@ public class TestJaxrsBase extends KillbillClient {
@Inject
protected PersistentBus internalBus;
+ @Inject
+ protected TestApiListener busHandler;
+
protected static TestKillbillGuiceListener listener;
protected CoreConfig config;
private HttpServer server;
- protected TestApiListener busHandler;
public static void loadSystemPropertiesFromClasspath(final String resource) {
final URL url = TestJaxrsBase.class.getResource(resource);
@@ -293,8 +295,6 @@ public class TestJaxrsBase extends KillbillClient {
loadConfig();
listener.getInstantiatedInjector().injectMembers(this);
-
- busHandler = new TestApiListener(null, dbi);
}
protected void loadConfig() {
diff --git a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestSubscriptionHelper.java b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestSubscriptionHelper.java
index 522a48e..d4571a8 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestSubscriptionHelper.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestSubscriptionHelper.java
@@ -35,7 +35,6 @@ import org.testng.Assert;
import com.ning.billing.ErrorCode;
import com.ning.billing.api.TestApiListener;
import com.ning.billing.api.TestApiListener.NextEvent;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.callcontext.InternalCallContext;
import com.ning.billing.catalog.api.BillingPeriod;
import com.ning.billing.catalog.api.Duration;
@@ -46,8 +45,6 @@ import com.ning.billing.catalog.api.ProductCategory;
import com.ning.billing.catalog.api.TimeUnit;
import com.ning.billing.clock.Clock;
import com.ning.billing.events.EffectiveSubscriptionInternalEvent;
-import com.ning.billing.subscription.SubscriptionTestListenerStatus;
-import com.ning.billing.subscription.SubscriptionTestSuiteWithEmbeddedDB;
import com.ning.billing.subscription.api.SubscriptionBaseInternalApi;
import com.ning.billing.subscription.api.SubscriptionBaseTransitionType;
import com.ning.billing.subscription.api.migration.SubscriptionBaseMigrationApi.AccountMigration;
@@ -83,17 +80,14 @@ public class TestSubscriptionHelper {
private final TestApiListener testListener;
- private final TestListenerStatus testListenerStatus;
-
private final SubscriptionDao dao;
@Inject
- public TestSubscriptionHelper(final SubscriptionBaseInternalApi subscriptionApi, final Clock clock, final InternalCallContext callContext, final TestApiListener testListener, final TestListenerStatus testListenerStatus, final SubscriptionDao dao) {
+ public TestSubscriptionHelper(final SubscriptionBaseInternalApi subscriptionApi, final Clock clock, final InternalCallContext callContext, final TestApiListener testListener, final SubscriptionDao dao) {
this.subscriptionApi = subscriptionApi;
this.clock = clock;
this.callContext = callContext;
this.testListener = testListener;
- this.testListenerStatus = testListenerStatus;
this.dao = dao;
}
@@ -115,8 +109,7 @@ public class TestSubscriptionHelper {
requestedDate == null ? clock.getUTCNow() : requestedDate, callContext);
assertNotNull(subscription);
- assertTrue(testListener.isCompleted(SubscriptionTestSuiteWithEmbeddedDB.DELAY));
- ((SubscriptionTestListenerStatus) testListenerStatus).assertListenerStatus();
+ testListener.assertListenerStatus();
return subscription;
}
diff --git a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiAddOn.java b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiAddOn.java
index e04b4d2..27f083b 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiAddOn.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiAddOn.java
@@ -161,7 +161,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE);
@@ -187,7 +186,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
assertTrue(aoSubscription.isSubscriptionFutureCancelled());
// MOVE AFTER CANCELLATION
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.CANCEL);
testListener.pushExpectedEvent(NextEvent.CANCEL);
@@ -217,7 +215,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE);
@@ -242,7 +239,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
assertEquals(aoSubscription.getState(), EntitlementState.ACTIVE);
assertTrue(aoSubscription.isSubscriptionFutureCancelled());
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.UNCANCEL);
baseSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(baseSubscription.getId(), internalCallContext);
baseSubscription.uncancel(callContext);
@@ -283,7 +279,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE);
@@ -314,7 +309,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
assertEquals(aoStatus.get(0).getPriceList(), aoSubscription.getCurrentPriceList().getName());
assertEquals(aoStatus.get(0).getReason(), DryRunChangeReason.AO_INCLUDED_IN_NEW_PLAN);
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.CHANGE);
testListener.pushExpectedEvent(NextEvent.CANCEL);
baseSubscription.changePlan(newBaseProduct, newBaseTerm, newBasePriceList, callContext);
@@ -340,9 +334,9 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
final BillingPeriod aoTerm = BillingPeriod.MONTHLY;
final String aoPriceList = PriceListSet.DEFAULT_PRICELIST_NAME;
+ // CREATE AO
DefaultSubscriptionBase aoSubscription = testUtil.createSubscription(bundle, aoProduct, aoTerm, aoPriceList);
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE);
@@ -381,7 +375,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
assertTrue(aoSubscription.isSubscriptionFutureCancelled());
// MOVE AFTER CHANGE
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.CHANGE);
testListener.pushExpectedEvent(NextEvent.CANCEL);
it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
@@ -410,8 +403,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
assertEquals(alignement, PlanAlignmentCreate.START_OF_BUNDLE);
testAddonCreateInternal(aoProduct, aoTerm, aoPriceList, alignement);
-
- assertListenerStatus();
}
@Test(groups = "slow")
@@ -429,8 +420,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
assertEquals(alignement, PlanAlignmentCreate.START_OF_SUBSCRIPTION);
testAddonCreateInternal(aoProduct, aoTerm, aoPriceList, alignement);
-
- assertListenerStatus();
}
private void testAddonCreateInternal(final String aoProduct, final BillingPeriod aoTerm, final String aoPriceList, final PlanAlignmentCreate expAlignement) throws SubscriptionBaseApiException {
@@ -473,7 +462,6 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
}
// ADD TWO PHASE EVENTS (BP + AO)
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.PHASE);
testListener.pushExpectedEvent(NextEvent.PHASE);
@@ -498,5 +486,7 @@ public class TestUserApiAddOn extends SubscriptionTestSuiteWithEmbeddedDB {
aoSubscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(aoSubscription.getId(), internalCallContext);
aoPendingTranstion = aoSubscription.getPendingTransition();
assertNull(aoPendingTranstion);
+
+ assertListenerStatus();
}
}
diff --git a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiCancel.java b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiCancel.java
index dd7d8d3..2731c83 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiCancel.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiCancel.java
@@ -33,7 +33,6 @@ import com.ning.billing.subscription.SubscriptionTestSuiteWithEmbeddedDB;
import com.ning.billing.subscription.api.SubscriptionBillingApiException;
import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
@@ -67,7 +66,7 @@ public class TestUserApiCancel extends SubscriptionTestSuiteWithEmbeddedDB {
// CANCEL in trial period to get IMM policy
subscription.cancel(callContext);
currentPhase = subscription.getCurrentPhase();
- testListener.isCompleted(3000);
+ assertListenerStatus();
assertEquals(subscription.getLastActiveProduct().getName(), prod);
assertEquals(subscription.getLastActivePriceList().getName(), planSet);
@@ -116,11 +115,8 @@ public class TestUserApiCancel extends SubscriptionTestSuiteWithEmbeddedDB {
assertEquals(subscription.getLastActiveCategory(), ProductCategory.BASE);
// CANCEL
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.CANCEL);
subscription.cancel(callContext);
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
assertEquals(subscription.getLastActiveProduct().getName(), prod);
assertEquals(subscription.getLastActivePriceList().getName(), planSet);
diff --git a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiChangePlan.java b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiChangePlan.java
index 4e43b10..3c2bc16 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiChangePlan.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/api/user/TestUserApiChangePlan.java
@@ -38,7 +38,6 @@ import com.ning.billing.subscription.events.SubscriptionBaseEvent;
import com.ning.billing.subscription.events.user.ApiEvent;
import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
@@ -123,12 +122,9 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
subscriptionInternalApi.setChargedThroughDate(subscription.getId(), newChargedThroughDate, internalCallContext);
// RE READ SUBSCRIPTION + CHANGE PLAN
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.CHANGE);
subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
subscription.changePlan(toProd, toTerm, toPlanSet, callContext);
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
// CHECK CHANGE PLAN
currentPhase = subscription.getCurrentPhase();
@@ -229,10 +225,7 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
checkChangePlan(subscription, fromProd, ProductCategory.BASE, fromTerm, PhaseType.EVERGREEN);
// CHECK CHANGE DID NOT KICK IN YET
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.CHANGE);
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
// MOVE TO AFTER CTD
testListener.pushExpectedEvent(NextEvent.CHANGE);
@@ -250,13 +243,10 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
assertEquals(currentPhase.getPhaseType(), PhaseType.DISCOUNT);
// MOVE TIME ABOUT ONE MONTH BEFORE NEXT EXPECTED PHASE CHANGE
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.PHASE);
it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(11));
clock.addDeltaFromReality(it.toDurationMillis());
currentTime = clock.getUTCNow();
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
final DateTime nextExpectedPhaseChange = TestSubscriptionHelper.addDuration(newChargedThroughDate, currentPhase.getDuration());
testUtil.checkNextPhaseChange(subscription, 1, nextExpectedPhaseChange);
@@ -296,11 +286,8 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
// CHANGE EOT
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.CHANGE);
subscription.changePlan("Pistol", BillingPeriod.MONTHLY, "gunclubDiscount", callContext);
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
// CHANGE
testListener.pushExpectedEvent(NextEvent.CHANGE);
@@ -341,18 +328,12 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
subscription = (DefaultSubscriptionBase) subscriptionInternalApi.getSubscriptionFromId(subscription.getId(), internalCallContext);
// CHANGE EOT
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.CHANGE);
subscription.changePlan("Shotgun", BillingPeriod.MONTHLY, "gunclubDiscount", callContext);
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
// CHANGE EOT
- testListener.setNonExpectedMode();
- testListener.pushExpectedEvent(NextEvent.CHANGE);
subscription.changePlan("Pistol", BillingPeriod.ANNUAL, "gunclubDiscount", callContext);
- assertFalse(testListener.isCompleted(3000));
- testListener.reset();
+ assertListenerStatus();
// CHECK NO CHANGE OCCURED YET
Plan currentPlan = subscription.getCurrentPlan();
@@ -365,11 +346,10 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
assertNotNull(currentPhase);
assertEquals(currentPhase.getPhaseType(), PhaseType.DISCOUNT);
- // ACTIVATE CHNAGE BY MOVING AFTER CTD
+ // ACTIVATE CHANGE BY MOVING AFTER CTD
testListener.pushExpectedEvent(NextEvent.CHANGE);
it = new Interval(clock.getUTCNow(), clock.getUTCNow().plusMonths(1));
clock.addDeltaFromReality(it.toDurationMillis());
-
assertListenerStatus();
currentPlan = subscription.getCurrentPlan();
@@ -413,11 +393,9 @@ public class TestUserApiChangePlan extends SubscriptionTestSuiteWithEmbeddedDB {
clock.addDeltaFromReality(it.toDurationMillis());
// CHANGE IMMEDIATE TO A 3 PHASES PLAN
- testListener.reset();
testListener.pushExpectedEvent(NextEvent.CHANGE);
subscription.changePlan("Assault-Rifle", BillingPeriod.ANNUAL, "gunclubDiscount", callContext);
assertListenerStatus();
- testListener.reset();
// CHECK EVERYTHING LOOKS CORRECT
final Plan currentPlan = subscription.getCurrentPlan();
diff --git a/subscription/src/test/java/com/ning/billing/subscription/DefaultSubscriptionTestInitializer.java b/subscription/src/test/java/com/ning/billing/subscription/DefaultSubscriptionTestInitializer.java
index 0bbc2b1..c48e9ba 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/DefaultSubscriptionTestInitializer.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/DefaultSubscriptionTestInitializer.java
@@ -25,7 +25,6 @@ import org.slf4j.LoggerFactory;
import com.ning.billing.account.api.AccountData;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.callcontext.InternalCallContext;
import com.ning.billing.catalog.DefaultCatalogService;
import com.ning.billing.catalog.api.Catalog;
@@ -85,13 +84,12 @@ public class DefaultSubscriptionTestInitializer implements SubscriptionTestIniti
}
public void startTestFamework(final TestApiListener testListener,
- final TestListenerStatus testListenerStatus,
final ClockMock clock,
final BusService busService,
final SubscriptionBaseService subscriptionBaseService) throws Exception {
log.debug("STARTING TEST FRAMEWORK");
- resetTestListener(testListener, testListenerStatus);
+ resetTestListener(testListener);
resetClockToStartOfTest(clock);
@@ -113,11 +111,10 @@ public class DefaultSubscriptionTestInitializer implements SubscriptionTestIniti
log.debug("STOPPED TEST FRAMEWORK");
}
- private void resetTestListener(final TestApiListener testListener, final TestListenerStatus testListenerStatus) {
+ private void resetTestListener(final TestApiListener testListener) {
// RESET LIST OF EXPECTED EVENTS
if (testListener != null) {
testListener.reset();
- testListenerStatus.resetTestListenerStatus();
}
}
diff --git a/subscription/src/test/java/com/ning/billing/subscription/glue/TestDefaultSubscriptionModule.java b/subscription/src/test/java/com/ning/billing/subscription/glue/TestDefaultSubscriptionModule.java
index c4a1020..93ad00f 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/glue/TestDefaultSubscriptionModule.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/glue/TestDefaultSubscriptionModule.java
@@ -21,11 +21,9 @@ import org.skife.config.ConfigSource;
import com.ning.billing.account.api.AccountUserApi;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.catalog.glue.CatalogModule;
import com.ning.billing.subscription.DefaultSubscriptionTestInitializer;
import com.ning.billing.subscription.SubscriptionTestInitializer;
-import com.ning.billing.subscription.SubscriptionTestListenerStatus;
import com.ning.billing.subscription.api.user.TestSubscriptionHelper;
import com.ning.billing.util.glue.CacheModule;
import com.ning.billing.util.glue.CallContextModule;
@@ -46,7 +44,6 @@ public class TestDefaultSubscriptionModule extends DefaultSubscriptionModule {
bind(AccountUserApi.class).toInstance(Mockito.mock(AccountUserApi.class));
bind(TestSubscriptionHelper.class).asEagerSingleton();
- bind(TestListenerStatus.class).to(SubscriptionTestListenerStatus.class).asEagerSingleton();
bind(TestApiListener.class).asEagerSingleton();
bind(SubscriptionTestInitializer.class).to(DefaultSubscriptionTestInitializer.class).asEagerSingleton();
}
diff --git a/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestInitializer.java b/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestInitializer.java
index da76f3b..001f093 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestInitializer.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestInitializer.java
@@ -18,14 +18,13 @@ package com.ning.billing.subscription;
import com.ning.billing.account.api.AccountData;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
+import com.ning.billing.callcontext.InternalCallContext;
import com.ning.billing.catalog.api.Catalog;
import com.ning.billing.catalog.api.CatalogService;
import com.ning.billing.clock.ClockMock;
+import com.ning.billing.subscription.api.SubscriptionBaseInternalApi;
import com.ning.billing.subscription.api.SubscriptionBaseService;
import com.ning.billing.subscription.api.user.SubscriptionBaseBundle;
-import com.ning.billing.callcontext.InternalCallContext;
-import com.ning.billing.subscription.api.SubscriptionBaseInternalApi;
import com.ning.billing.util.svcsapi.bus.BusService;
public interface SubscriptionTestInitializer {
@@ -37,7 +36,6 @@ public interface SubscriptionTestInitializer {
public SubscriptionBaseBundle initBundle(final SubscriptionBaseInternalApi subscriptionApi, final InternalCallContext callContext) throws Exception;
public void startTestFamework(final TestApiListener testListener,
- final TestListenerStatus testListenerStatus,
final ClockMock clock,
final BusService busService,
final SubscriptionBaseService subscriptionBaseService) throws Exception;
diff --git a/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteNoDB.java b/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteNoDB.java
index 9028afc..2ba7c8d 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteNoDB.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteNoDB.java
@@ -33,7 +33,6 @@ import org.testng.annotations.BeforeMethod;
import com.ning.billing.GuicyKillbillTestSuiteNoDB;
import com.ning.billing.account.api.AccountData;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.catalog.api.Catalog;
import com.ning.billing.catalog.api.CatalogService;
import com.ning.billing.clock.ClockMock;
@@ -88,8 +87,6 @@ public class SubscriptionTestSuiteNoDB extends GuicyKillbillTestSuiteNoDB {
protected TestSubscriptionHelper testUtil;
@Inject
protected TestApiListener testListener;
- @Inject
- protected TestListenerStatus testListenerStatus;
@Inject
protected SubscriptionTestInitializer subscriptionTestInitializer;
@@ -122,7 +119,7 @@ public class SubscriptionTestSuiteNoDB extends GuicyKillbillTestSuiteNoDB {
// CLEANUP ALL DB TABLES OR IN MEMORY STRUCTURES
((MockSubscriptionDaoMemory) dao).reset();
- subscriptionTestInitializer.startTestFamework(testListener, testListenerStatus, clock, busService, subscriptionBaseService);
+ subscriptionTestInitializer.startTestFamework(testListener, clock, busService, subscriptionBaseService);
this.catalog = subscriptionTestInitializer.initCatalog(catalogService);
this.accountData = subscriptionTestInitializer.initAccountData();
@@ -135,6 +132,6 @@ public class SubscriptionTestSuiteNoDB extends GuicyKillbillTestSuiteNoDB {
}
protected void assertListenerStatus() {
- ((SubscriptionTestListenerStatus) testListenerStatus).assertListenerStatus();
+ testListener.assertListenerStatus();
}
}
diff --git a/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteWithEmbeddedDB.java b/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteWithEmbeddedDB.java
index e6da5ea..89e53d9 100644
--- a/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteWithEmbeddedDB.java
+++ b/subscription/src/test/java/com/ning/billing/subscription/SubscriptionTestSuiteWithEmbeddedDB.java
@@ -30,7 +30,6 @@ import org.testng.annotations.BeforeMethod;
import com.ning.billing.GuicyKillbillTestSuiteWithEmbeddedDB;
import com.ning.billing.account.api.AccountData;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.catalog.api.Catalog;
import com.ning.billing.catalog.api.CatalogService;
import com.ning.billing.clock.ClockMock;
@@ -50,8 +49,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Stage;
-import static org.testng.Assert.assertTrue;
-
public class SubscriptionTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWithEmbeddedDB {
protected static final Logger log = LoggerFactory.getLogger(SubscriptionTestSuiteWithEmbeddedDB.class);
@@ -86,8 +83,6 @@ public class SubscriptionTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteW
@Inject
protected TestApiListener testListener;
@Inject
- protected TestListenerStatus testListenerStatus;
- @Inject
protected SubscriptionTestInitializer subscriptionTestInitializer;
protected Catalog catalog;
@@ -113,7 +108,7 @@ public class SubscriptionTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteW
@BeforeMethod(groups = "slow")
public void beforeMethod() throws Exception {
super.beforeMethod();
- subscriptionTestInitializer.startTestFamework(testListener, testListenerStatus, clock, busService, subscriptionBaseService);
+ subscriptionTestInitializer.startTestFamework(testListener, clock, busService, subscriptionBaseService);
this.catalog = subscriptionTestInitializer.initCatalog(catalogService);
this.accountData = subscriptionTestInitializer.initAccountData();
@@ -132,7 +127,6 @@ public class SubscriptionTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteW
}
protected void assertListenerStatus() {
- assertTrue(testListener.isCompleted(DELAY));
- ((SubscriptionTestListenerStatus) testListenerStatus).assertListenerStatus();
+ testListener.assertListenerStatus();
}
}
diff --git a/util/src/test/java/com/ning/billing/api/TestApiListener.java b/util/src/test/java/com/ning/billing/api/TestApiListener.java
index 1f8aacf..3cd1d49 100644
--- a/util/src/test/java/com/ning/billing/api/TestApiListener.java
+++ b/util/src/test/java/com/ning/billing/api/TestApiListener.java
@@ -20,6 +20,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@@ -29,6 +30,7 @@ import org.skife.jdbi.v2.IDBI;
import org.skife.jdbi.v2.tweak.HandleCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.testng.Assert;
import com.ning.billing.events.BlockingTransitionInternalEvent;
import com.ning.billing.events.CustomFieldEvent;
@@ -47,30 +49,45 @@ import com.google.common.base.Joiner;
import com.google.common.eventbus.Subscribe;
import static com.jayway.awaitility.Awaitility.await;
-import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
public class TestApiListener {
- protected static final Logger log = LoggerFactory.getLogger(TestApiListener.class);
+ private static final Logger log = LoggerFactory.getLogger(TestApiListener.class);
- private final List<NextEvent> nextExpectedEvent;
+ private static final Joiner SPACE_JOINER = Joiner.on(" ");
+
+ private static final long DELAY = 25000;
- private final TestListenerStatus testStatus;
+ private final List<NextEvent> nextExpectedEvent;
private final IDBI idbi;
- private boolean nonExpectedMode;
+ private boolean isListenerFailed = false;
+ private String listenerFailedMsg;
private volatile boolean completed;
@Inject
- public TestApiListener(final TestListenerStatus testStatus, final IDBI idbi) {
+ public TestApiListener(final IDBI idbi) {
nextExpectedEvent = new Stack<NextEvent>();
this.completed = false;
- this.testStatus = testStatus;
- this.nonExpectedMode = false;
this.idbi = idbi;
}
+ public void assertListenerStatus() {
+ try {
+ assertTrue(isCompleted(DELAY));
+ } catch (final Exception e) {
+ fail("assertListenerStatus didn't complete", e);
+ }
+
+ if (isListenerFailed) {
+ log.error(listenerFailedMsg);
+ Assert.fail(listenerFailedMsg);
+ }
+ }
+
public enum NextEvent {
MIGRATE_ENTITLEMENT,
MIGRATE_BILLING,
@@ -95,12 +112,6 @@ public class TestApiListener {
CUSTOM_FIELD,
}
- public void setNonExpectedMode() {
- synchronized (this) {
- this.nonExpectedMode = true;
- }
- }
-
@Subscribe
public void handleRepairSubscriptionEvents(final RepairSubscriptionInternalEvent event) {
log.info(String.format("Got RepairSubscriptionEvent event %s", event.toString()));
@@ -189,7 +200,6 @@ public class TestApiListener {
notifyIfStackEmpty();
}
-
@Subscribe
public synchronized void processTagDefinitonEvent(final TagDefinitionInternalEvent event) {
log.info(String.format("Got TagDefinitionInternalEvent event %s", event.toString()));
@@ -236,7 +246,9 @@ public class TestApiListener {
synchronized (this) {
nextExpectedEvent.clear();
completed = true;
- nonExpectedMode = false;
+
+ isListenerFailed = false;
+ listenerFailedMsg = null;
}
}
@@ -248,9 +260,8 @@ public class TestApiListener {
public void pushExpectedEvent(final NextEvent next) {
synchronized (this) {
- final Joiner joiner = Joiner.on(" ");
nextExpectedEvent.add(next);
- log.debug("Stacking expected event {}, got [{}]", next, joiner.join(nextExpectedEvent));
+ log.debug("Stacking expected event {}, got [{}]", next, SPACE_JOINER.join(nextExpectedEvent));
completed = false;
}
}
@@ -271,7 +282,7 @@ public class TestApiListener {
// all expected events. But other handlers might still be processing them.
// Since there is only one bus thread, and that the test thread waits for all events to be processed,
// we're guaranteed that all are processed when the bus events table is empty.
- await().atMost(10, SECONDS).until(new Callable<Boolean>() {
+ await().atMost(timeout, TimeUnit.MILLISECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
final long inProcessingBusEvents = idbi.withHandle(new HandleCallback<Long>() {
@@ -288,16 +299,18 @@ public class TestApiListener {
}
final DateTime after = new DateTime();
waitTimeMs -= after.getMillis() - before.getMillis();
- } catch (Exception ignore) {
+ } catch (final Exception ignore) {
log.error("isCompleted got interrupted ", ignore);
return false;
}
} while (waitTimeMs > 0 && !completed);
}
- if (!completed && !nonExpectedMode) {
+
+ if (!completed) {
final Joiner joiner = Joiner.on(" ");
log.error("TestApiListener did not complete in " + timeout + " ms, remaining events are " + joiner.join(nextExpectedEvent));
}
+
return completed;
}
@@ -323,21 +336,19 @@ public class TestApiListener {
if (ev == received) {
it.remove();
foundIt = true;
- if (!nonExpectedMode) {
- log.debug("Found expected event {}. Yeah!", received);
- } else {
- log.error("Found non expected event {}. Boohh! ", received);
- }
+ log.debug("Found expected event {}. Yeah!", received);
break;
}
}
- if (!foundIt && !nonExpectedMode) {
- final Joiner joiner = Joiner.on(" ");
- log.error("Received unexpected event " + received + "; remaining expected events [" + joiner.join(nextExpectedEvent) + "]");
- if (testStatus != null) {
- testStatus.failed("TestApiListener [ApiListenerStatus]: Received unexpected event " + received + "; remaining expected events [" + joiner.join(nextExpectedEvent) + "]");
- }
+ if (!foundIt) {
+ log.error("Received unexpected event " + received + "; remaining expected events [" + SPACE_JOINER.join(nextExpectedEvent) + "]");
+ failed("TestApiListener [ApiListenerStatus]: Received unexpected event " + received + "; remaining expected events [" + SPACE_JOINER.join(nextExpectedEvent) + "]");
}
}
}
+
+ private void failed(final String msg) {
+ this.isListenerFailed = true;
+ this.listenerFailedMsg = msg;
+ }
}
diff --git a/util/src/test/java/com/ning/billing/util/glue/TestUtilModuleWithEmbeddedDB.java b/util/src/test/java/com/ning/billing/util/glue/TestUtilModuleWithEmbeddedDB.java
index dd0c9af..5ed326c 100644
--- a/util/src/test/java/com/ning/billing/util/glue/TestUtilModuleWithEmbeddedDB.java
+++ b/util/src/test/java/com/ning/billing/util/glue/TestUtilModuleWithEmbeddedDB.java
@@ -20,6 +20,7 @@ import org.skife.config.ConfigSource;
import com.ning.billing.DBTestingHelper;
import com.ning.billing.GuicyKillbillTestWithEmbeddedDBModule;
+import com.ning.billing.api.TestApiListener;
import com.ning.billing.util.globallocker.TestGlobalLockerModule;
public class TestUtilModuleWithEmbeddedDB extends TestUtilModule {
@@ -41,5 +42,7 @@ public class TestUtilModuleWithEmbeddedDB extends TestUtilModule {
install(new NotificationQueueModule(configSource));
install(new NonEntityDaoModule());
install(new TestGlobalLockerModule(DBTestingHelper.get()));
+
+ bind(TestApiListener.class).asEagerSingleton();
}
}
diff --git a/util/src/test/java/com/ning/billing/util/UtilTestSuiteWithEmbeddedDB.java b/util/src/test/java/com/ning/billing/util/UtilTestSuiteWithEmbeddedDB.java
index ac75097..28d45a0 100644
--- a/util/src/test/java/com/ning/billing/util/UtilTestSuiteWithEmbeddedDB.java
+++ b/util/src/test/java/com/ning/billing/util/UtilTestSuiteWithEmbeddedDB.java
@@ -21,14 +21,12 @@ import javax.inject.Inject;
import org.skife.jdbi.v2.IDBI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import com.ning.billing.GuicyKillbillTestSuiteWithEmbeddedDB;
import com.ning.billing.api.TestApiListener;
-import com.ning.billing.api.TestListenerStatus;
import com.ning.billing.bus.api.PersistentBus;
import com.ning.billing.commons.locker.GlobalLocker;
import com.ning.billing.notificationq.api.NotificationQueueService;
@@ -47,14 +45,10 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Stage;
-import static org.testng.Assert.assertTrue;
-
-public abstract class UtilTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWithEmbeddedDB implements TestListenerStatus {
+public abstract class UtilTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuiteWithEmbeddedDB {
private static final Logger log = LoggerFactory.getLogger(UtilTestSuiteWithEmbeddedDB.class);
- protected static final long DELAY = 10000;
-
@Inject
protected PersistentBus eventBus;
@Inject
@@ -81,18 +75,13 @@ public abstract class UtilTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuite
protected GlobalLocker locker;
@Inject
protected IDBI idbi;
-
+ @Inject
protected TestApiListener eventsListener;
- private boolean isListenerFailed;
- private String listenerFailedMsg;
-
@BeforeClass(groups = "slow")
public void beforeClass() throws Exception {
final Injector g = Guice.createInjector(Stage.PRODUCTION, new TestUtilModuleWithEmbeddedDB(configSource));
g.injectMembers(this);
-
- eventsListener = new TestApiListener(this, idbi);
}
@Override
@@ -100,7 +89,6 @@ public abstract class UtilTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuite
public void beforeMethod() throws Exception {
super.beforeMethod();
- resetTestListenerStatus();
eventsListener.reset();
eventBus.start();
@@ -121,23 +109,7 @@ public abstract class UtilTestSuiteWithEmbeddedDB extends GuicyKillbillTestSuite
eventBus.stop();
}
- @Override
- public void failed(final String msg) {
- isListenerFailed = true;
- listenerFailedMsg = msg;
- }
-
- @Override
- public void resetTestListenerStatus() {
- isListenerFailed = false;
- listenerFailedMsg = null;
- }
-
protected void assertListenerStatus() {
- assertTrue(eventsListener.isCompleted(DELAY));
- if (isListenerFailed) {
- log.error(listenerFailedMsg);
- Assert.fail(listenerFailedMsg);
- }
+ eventsListener.assertListenerStatus();
}
}