polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [2/2] polygene-java git commit: riak: wait for Riak to be ready when running docker based tests
Date Sat, 25 Feb 2017 17:25:21 GMT
riak: wait for Riak to be ready when running docker based tests


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/120a15ce
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/120a15ce
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/120a15ce

Branch: refs/heads/develop
Commit: 120a15ced0eab7b69d3bc13e0c9dbd34a421396d
Parents: cdd2719
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Sat Feb 25 17:34:49 2017 +0100
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Sat Feb 25 17:34:49 2017 +0100

----------------------------------------------------------------------
 extensions/entitystore-riak/build.gradle        |   1 +
 .../entitystore/riak/RiakAccessors.java         |   5 +-
 .../riak/RiakMapEntityStoreMixin.java           |   4 +-
 .../polygene/entitystore/riak/RiakFixture.java  | 108 +++++++++++++++++++
 .../riak/RiakMapEntityStoreTest.java            |  63 +++--------
 .../riak/RiakMapEntityStoreWithCacheTest.java   |  61 +++--------
 6 files changed, 145 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/120a15ce/extensions/entitystore-riak/build.gradle
----------------------------------------------------------------------
diff --git a/extensions/entitystore-riak/build.gradle b/extensions/entitystore-riak/build.gradle
index 0fa0787..a36ca6c 100644
--- a/extensions/entitystore-riak/build.gradle
+++ b/extensions/entitystore-riak/build.gradle
@@ -35,6 +35,7 @@ dependencies {
 
   testImplementation polygene.internals.testsupport
   testImplementation polygene.extension( 'valueserialization-orgjson' )
+  testImplementation libraries.awaitility
 
   testRuntimeOnly libraries.logback
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/120a15ce/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakAccessors.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakAccessors.java
b/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakAccessors.java
index 2e17604..3efef81 100644
--- a/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakAccessors.java
+++ b/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakAccessors.java
@@ -20,12 +20,11 @@
 package org.apache.polygene.entitystore.riak;
 
 import com.basho.riak.client.api.RiakClient;
+import com.basho.riak.client.core.query.Namespace;
 
 public interface RiakAccessors
 {
-
     RiakClient riakClient();
 
-    String bucket();
-
+    Namespace riakNamespace();
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/120a15ce/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreMixin.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreMixin.java
b/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreMixin.java
index df18dae..bb461cd 100644
--- a/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreMixin.java
+++ b/extensions/entitystore-riak/src/main/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreMixin.java
@@ -214,9 +214,9 @@ public class RiakMapEntityStoreMixin implements ServiceActivation, MapEntityStor
     }
 
     @Override
-    public String bucket()
+    public Namespace riakNamespace()
     {
-        return namespace.getBucketNameAsString();
+        return namespace;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/120a15ce/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakFixture.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakFixture.java
b/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakFixture.java
new file mode 100644
index 0000000..4e05602
--- /dev/null
+++ b/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakFixture.java
@@ -0,0 +1,108 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.polygene.entitystore.riak;
+
+import com.basho.riak.client.api.RiakClient;
+import com.basho.riak.client.api.commands.kv.DeleteValue;
+import com.basho.riak.client.api.commands.kv.ListKeys;
+import com.basho.riak.client.api.commands.kv.StoreValue;
+import com.basho.riak.client.core.query.Location;
+import com.basho.riak.client.core.query.Namespace;
+import java.time.Instant;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import org.awaitility.Awaitility;
+import org.awaitility.Duration;
+
+class RiakFixture
+{
+    private final RiakClient client;
+    private final Namespace namespace;
+
+    RiakFixture( RiakClient client, Namespace namespace )
+    {
+        this.client = client;
+        this.namespace = namespace;
+    }
+
+    void waitUntilReady()
+    {
+        System.out.println( ">> Riak HealthCheck BEGIN" );
+        Instant start = Instant.now();
+        Awaitility.await()
+                  .pollDelay( Duration.ZERO )
+                  .pollInterval( Duration.ONE_SECOND )
+                  .timeout( Duration.ONE_MINUTE )
+                  .until( new HealthCheck() );
+        System.out.println( ">> Riak HealthCheck END, took " + java.time.Duration.between(
start, Instant.now() ) );
+    }
+
+    void deleteTestData() throws ExecutionException, InterruptedException
+    {
+        // Riak doesn't expose bucket deletion in its API so we empty it
+        if( namespace != null )
+        {
+            ListKeys listKeys = new ListKeys.Builder( namespace ).build();
+            ListKeys.Response listKeysResponse = client.execute( listKeys );
+            for( Location location : listKeysResponse )
+            {
+                DeleteValue delete = new DeleteValue.Builder( location ).build();
+                client.execute( delete );
+            }
+        }
+    }
+
+    private class HealthCheck implements Callable<Boolean>
+    {
+        @Override
+        public Boolean call()
+        {
+            boolean inserted = false;
+            boolean deleted = false;
+            Location location = new Location( namespace, "HEALTH_CHECK_ID" );
+            try
+            {
+                StoreValue store = new StoreValue.Builder( "DATA" ).withLocation( location
).build();
+                client.execute( store );
+                inserted = true;
+            }
+            catch( Throwable ex )
+            {
+                ex.printStackTrace();
+                return false;
+            }
+            finally
+            {
+                if( inserted )
+                {
+                    try
+                    {
+                        DeleteValue delete = new DeleteValue.Builder( location ).build();
+                        client.execute( delete );
+                        deleted = true;
+                    }
+                    catch( Throwable ex )
+                    {
+                        ex.printStackTrace();
+                    }
+                }
+            }
+            return inserted && deleted;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/120a15ce/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreTest.java
b/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreTest.java
index bf4de29..54a8f81 100644
--- a/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreTest.java
+++ b/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreTest.java
@@ -17,43 +17,42 @@
  */
 package org.apache.polygene.entitystore.riak;
 
-import com.basho.riak.client.api.RiakClient;
-import com.basho.riak.client.api.commands.kv.DeleteValue;
-import com.basho.riak.client.api.commands.kv.ListKeys;
-import com.basho.riak.client.core.query.Location;
-import com.basho.riak.client.core.query.Namespace;
 import java.util.Collections;
 import org.apache.polygene.api.common.Visibility;
-import org.apache.polygene.bootstrap.AssemblyException;
 import org.apache.polygene.bootstrap.ModuleAssembly;
 import org.apache.polygene.entitystore.riak.assembly.RiakEntityStoreAssembler;
 import org.apache.polygene.test.EntityTestAssembler;
 import org.apache.polygene.test.entity.AbstractEntityStoreTest;
 import org.apache.polygene.test.internal.DockerRule;
 import org.apache.polygene.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
-import org.junit.BeforeClass;
 import org.junit.ClassRule;
 
-public class RiakMapEntityStoreTest
-    extends AbstractEntityStoreTest
+public class RiakMapEntityStoreTest extends AbstractEntityStoreTest
 {
     @ClassRule
     public static final DockerRule DOCKER = new DockerRule( "riak", 8087 );
 
-    @BeforeClass
-    public static void waitForRiak() throws InterruptedException
+    private RiakFixture riakFixture;
+
+    @Override
+    public void setUp() throws Exception
     {
-        // TODO:flakiness Properly wait for Riak to be ready
-        // Riak listen to its network port before being ready to serve clients
-        // This breaks the test-support integration
-        // Wait a bit to give it a chance to be ready
-        Thread.sleep( 10_000 );
+        super.setUp();
+        RiakMapEntityStoreService es = serviceFinder.findService( RiakMapEntityStoreService.class
).get();
+        riakFixture = new RiakFixture( es.riakClient(), es.riakNamespace() );
+        riakFixture.waitUntilReady();
+    }
+
+    @Override
+    public void tearDown() throws Exception
+    {
+        riakFixture.deleteTestData();
+        super.tearDown();
     }
 
     @Override
     // START SNIPPET: assembly
     public void assemble( ModuleAssembly module )
-        throws AssemblyException
     {
         // END SNIPPET: assembly
         super.assemble( module );
@@ -71,34 +70,4 @@ public class RiakMapEntityStoreTest
         // START SNIPPET: assembly
     }
     // END SNIPPET: assembly
-
-    private RiakClient riakClient;
-    private String bucketKey;
-
-    @Override
-    public void setUp() throws Exception
-    {
-        super.setUp();
-        RiakMapEntityStoreService es = serviceFinder.findService( RiakMapEntityStoreService.class
).get();
-        riakClient = es.riakClient();
-        bucketKey = es.bucket();
-    }
-
-    @Override
-    public void tearDown() throws Exception
-    {
-        // Riak don't expose bucket deletion in its API so we empty the Polygene Entities
bucket.
-        if( bucketKey != null )
-        {
-            Namespace namespace = new Namespace( bucketKey );
-            ListKeys listKeys = new ListKeys.Builder( namespace ).build();
-            ListKeys.Response listKeysResponse = riakClient.execute( listKeys );
-            for( Location location : listKeysResponse )
-            {
-                DeleteValue delete = new DeleteValue.Builder( location ).build();
-                riakClient.execute( delete );
-            }
-        }
-        super.tearDown();
-    }
 }

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/120a15ce/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
----------------------------------------------------------------------
diff --git a/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
b/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
index 50759f9..85946cd 100644
--- a/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
+++ b/extensions/entitystore-riak/src/test/java/org/apache/polygene/entitystore/riak/RiakMapEntityStoreWithCacheTest.java
@@ -19,11 +19,6 @@
  */
 package org.apache.polygene.entitystore.riak;
 
-import com.basho.riak.client.api.RiakClient;
-import com.basho.riak.client.api.commands.kv.DeleteValue;
-import com.basho.riak.client.api.commands.kv.ListKeys;
-import com.basho.riak.client.core.query.Location;
-import com.basho.riak.client.core.query.Namespace;
 import java.util.Collections;
 import org.apache.polygene.api.common.Visibility;
 import org.apache.polygene.bootstrap.AssemblyException;
@@ -33,23 +28,29 @@ import org.apache.polygene.test.EntityTestAssembler;
 import org.apache.polygene.test.cache.AbstractEntityStoreWithCacheTest;
 import org.apache.polygene.test.internal.DockerRule;
 import org.apache.polygene.valueserialization.orgjson.OrgJsonValueSerializationAssembler;
-import org.junit.BeforeClass;
 import org.junit.ClassRule;
 
-public class RiakMapEntityStoreWithCacheTest
-    extends AbstractEntityStoreWithCacheTest
+public class RiakMapEntityStoreWithCacheTest extends AbstractEntityStoreWithCacheTest
 {
     @ClassRule
     public static final DockerRule DOCKER = new DockerRule( "riak", 8087 );
 
-    @BeforeClass
-    public static void waitForRiak() throws InterruptedException
+    private RiakFixture riakFixture;
+
+    @Override
+    public void setUp() throws Exception
     {
-        // TODO:flakiness Properly wait for Riak to be ready
-        // Riak listen to its network port before being ready to serve clients
-        // This breaks the test-support integration
-        // Wait a bit to give it a chance to be ready
-        Thread.sleep( 10_000 );
+        super.setUp();
+        RiakMapEntityStoreService es = serviceFinder.findService( RiakMapEntityStoreService.class
).get();
+        riakFixture = new RiakFixture( es.riakClient(), es.riakNamespace() );
+        riakFixture.waitUntilReady();
+    }
+
+    @Override
+    public void tearDown() throws Exception
+    {
+        riakFixture.deleteTestData();
+        super.tearDown();
     }
 
     @Override
@@ -67,34 +68,4 @@ public class RiakMapEntityStoreWithCacheTest
         int port = DOCKER.getExposedContainerPort( "8087/tcp" );
         riakConfig.hosts().set( Collections.singletonList( host + ':' + port ) );
     }
-
-    private RiakClient riakClient;
-    private String bucketKey;
-
-    @Override
-    public void setUp() throws Exception
-    {
-        super.setUp();
-        RiakMapEntityStoreService es = serviceFinder.findService( RiakMapEntityStoreService.class
).get();
-        riakClient = es.riakClient();
-        bucketKey = es.bucket();
-    }
-
-    @Override
-    public void tearDown() throws Exception
-    {
-        // Riak don't expose bucket deletion in its API so we empty the Polygene Entities
bucket.
-        if( bucketKey != null )
-        {
-            Namespace namespace = new Namespace( bucketKey );
-            ListKeys listKeys = new ListKeys.Builder( namespace ).build();
-            ListKeys.Response listKeysResponse = riakClient.execute( listKeys );
-            for( Location location : listKeysResponse )
-            {
-                DeleteValue delete = new DeleteValue.Builder( location ).build();
-                riakClient.execute( delete );
-            }
-        }
-        super.tearDown();
-    }
 }


Mime
View raw message