Search in sources :

Example 1 with TestSqliteDatabase

use of org.activityinfo.server.database.TestSqliteDatabase in project activityinfo by bedatadriven.

the class SyncIntegrationTest method failResume.

// AI-864 : we know that
// 1) on customer side location is present but locationadminlink entry is absent.
// 2) location and locationadminlink are updated with single SyncRegion
// Conclusion: the only possible bug is that location was updated but locationadminlink failed to update
// due to some weird problem (network connection failure)
// Test: in this test we will try to emulate connection failure
@Test
@OnDataSet("/dbunit/sites-simple-with-unicode.db.xml")
public void failResume() throws SQLException, InterruptedException {
    String databaseName = TestOutput.getFile(getClass(), ".sqlite").getAbsolutePath();
    final AtomicBoolean forceFail = new AtomicBoolean(true);
    final TestSqliteDatabase localDatabase = new TestSqliteDatabase(databaseName) {

        @Override
        public String adjustExecuteUpdates(String json) {
            JsonParser parser = new JsonParser();
            JsonArray list = parser.parse(json).getAsJsonArray();
            // ugly : better way to identify when to fail ?
            if (list.size() == 2 && json.contains("location") && json.contains("locationadminlink") && forceFail.get()) {
                forceFail.set(false);
                throw new RuntimeException("Forced to fail locationadminlink update");
            }
            return json;
        }
    };
    Dispatcher remoteDispatcher = new RemoteDispatcherStub(servlet);
    Injector clientSideInjector = Guice.createInjector(new LocalModuleStub(AuthenticationModuleStub.getCurrentUser(), localDatabase, remoteDispatcher));
    final InstallPipeline installer = clientSideInjector.getInstance(InstallPipeline.class);
    // sync with failure
    newRequest();
    installer.start();
    localDatabase.processEventQueue();
    // try again (now without failure)
    JdbcScheduler.get().forceCleanup();
    newRequest();
    installer.start();
    localDatabase.processEventQueue();
    assertThat(localDatabase.selectString("select Name from Location where LocationId=7"), equalTo("Shabunda"));
    assertThat(localDatabase.selectString(adminEntityBy(7, 1)), equalTo("3"));
    assertThat(localDatabase.selectString(adminEntityBy(7, 2)), equalTo("12"));
}
Also used : JsonArray(com.google.gson.JsonArray) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSqliteDatabase(org.activityinfo.server.database.TestSqliteDatabase) InstallPipeline(org.activityinfo.ui.client.local.sync.pipeline.InstallPipeline) Injector(com.google.inject.Injector) Dispatcher(org.activityinfo.ui.client.dispatch.Dispatcher) LocalModuleStub(org.activityinfo.ui.client.local.LocalModuleStub) JsonParser(com.google.gson.JsonParser) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 2 with TestSqliteDatabase

use of org.activityinfo.server.database.TestSqliteDatabase in project activityinfo by bedatadriven.

the class SyncIntegrationTest method syncWithHugeLocationsCount.

// AI-864, create 50k locations and try to sync them
// Check response time (must be less than 5seconds)
@Test
// we don't want to kill our build time, please run it manually
@Ignore
@OnDataSet("/dbunit/sites-simple-with-unicode.db.xml")
public void syncWithHugeLocationsCount() throws SQLException, InterruptedException {
    final TestSqliteDatabase localDatabase = new TestSqliteDatabase("target/localdbtest" + new java.util.Date().getTime());
    // before sync, fill in db with locations
    int generatedLocationCount = 50000;
    final List<Integer> locationIds = addLocationsToServerDatabase(generatedLocationCount);
    Dispatcher remoteDispatcher = new RemoteDispatcherStub(servlet);
    Injector clientSideInjector = Guice.createInjector(new LocalModuleStub(AuthenticationModuleStub.getCurrentUser(), localDatabase, remoteDispatcher));
    final InstallPipeline installer = clientSideInjector.getInstance(InstallPipeline.class);
    // sync
    newRequest();
    installer.start();
    localDatabase.processEventQueue();
    int locationCountInDataSet = 7;
    assertThat(localDatabase.selectInt("select count(*) from Location"), equalTo(generatedLocationCount + locationCountInDataSet));
    assertThat(localDatabase.selectString("select Name from Location where LocationId=7"), equalTo("Shabunda"));
    assertThat(localDatabase.selectString(adminEntityBy(7, 1)), equalTo("3"));
    assertThat(localDatabase.selectString(adminEntityBy(7, 2)), equalTo("12"));
    // assert all locations are persisted
    for (Integer id : locationIds) {
        assertThat(localDatabase.selectInt("select LocationId from Location where LocationId=" + id), equalTo(id));
    }
}
Also used : TestSqliteDatabase(org.activityinfo.server.database.TestSqliteDatabase) InstallPipeline(org.activityinfo.ui.client.local.sync.pipeline.InstallPipeline) Date(java.util.Date) Injector(com.google.inject.Injector) Dispatcher(org.activityinfo.ui.client.dispatch.Dispatcher) LocalModuleStub(org.activityinfo.ui.client.local.LocalModuleStub) Ignore(org.junit.Ignore) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

Injector (com.google.inject.Injector)2 OnDataSet (org.activityinfo.server.database.OnDataSet)2 TestSqliteDatabase (org.activityinfo.server.database.TestSqliteDatabase)2 Dispatcher (org.activityinfo.ui.client.dispatch.Dispatcher)2 LocalModuleStub (org.activityinfo.ui.client.local.LocalModuleStub)2 InstallPipeline (org.activityinfo.ui.client.local.sync.pipeline.InstallPipeline)2 Test (org.junit.Test)2 JsonArray (com.google.gson.JsonArray)1 JsonParser (com.google.gson.JsonParser)1 Date (java.util.Date)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Ignore (org.junit.Ignore)1