http://git-wip-us.apache.org/repos/asf/curator/blob/1fcb63a5/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java deleted file mode 100644 index d54ef6f..0000000 --- a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledCuratorFramework.java +++ /dev/null @@ -1,122 +0,0 @@ -/** - * 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.curator.x.async.modeled; - -import com.google.common.collect.Sets; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.retry.RetryOneTime; -import org.apache.curator.utils.CloseableUtils; -import org.apache.curator.x.async.AsyncStage; -import org.apache.curator.x.async.CompletableBaseClassForTests; -import org.apache.curator.x.async.modeled.models.TestModel; -import org.apache.curator.x.async.modeled.models.TestNewerModel; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import java.math.BigInteger; -import java.util.Set; -import java.util.concurrent.CountDownLatch; - -public class TestModeledCuratorFramework extends CompletableBaseClassForTests -{ - private static final ZPath path = ZPath.parse("/test/path"); - private CuratorFramework rawClient; - private JacksonModelSerializer<TestModel> serializer; - private JacksonModelSerializer<TestNewerModel> newSerializer; - private ModelSpec<TestModel> modelSpec; - private ModelSpec<TestNewerModel> newModelSpec; - - @BeforeMethod - @Override - public void setup() throws Exception - { - super.setup(); - - rawClient = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); - rawClient.start(); - - serializer = new JacksonModelSerializer<>(TestModel.class); - newSerializer = new JacksonModelSerializer<>(TestNewerModel.class); - - modelSpec = ModelSpec.builder(path, serializer).build(); - newModelSpec = ModelSpec.builder(path, newSerializer).build(); - } - - @AfterMethod - @Override - public void teardown() throws Exception - { - CloseableUtils.closeQuietly(rawClient); - super.teardown(); - } - - @Test - public void testCrud() - { - TestModel rawModel = new TestModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1)); - TestModel rawModel2 = new TestModel("Wayne", "Rooney", "Old Trafford", 10, BigInteger.valueOf(1)); - ModeledCuratorFramework<TestModel> client = ModeledCuratorFramework.wrap(rawClient, modelSpec); - AsyncStage<String> stage = client.set(rawModel); - Assert.assertNull(stage.event()); - complete(stage, (s, e) -> Assert.assertNotNull(s)); - complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel)); - complete(client.update(rawModel2)); - complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel2)); - complete(client.delete()); - complete(client.checkExists(), (stat, e) -> Assert.assertNull(stat)); - } - - @Test - public void testBackwardCompatibility() - { - TestNewerModel rawNewModel = new TestNewerModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1), 100); - ModeledCuratorFramework<TestNewerModel> clientForNew = ModeledCuratorFramework.wrap(rawClient, newModelSpec); - complete(clientForNew.set(rawNewModel), (s, e) -> Assert.assertNotNull(s)); - - ModeledCuratorFramework<TestModel> clientForOld = ModeledCuratorFramework.wrap(rawClient, modelSpec); - complete(clientForOld.read(), (model, e) -> Assert.assertTrue(rawNewModel.equalsOld(model))); - } - - @Test - public void testWatched() throws InterruptedException - { - CountDownLatch latch = new CountDownLatch(1); - ModeledCuratorFramework<TestModel> client = ModeledCuratorFramework.builder(rawClient, modelSpec).watched().build(); - client.checkExists().event().whenComplete((event, ex) -> latch.countDown()); - timing.sleepABit(); - Assert.assertEquals(latch.getCount(), 1); - client.set(new TestModel()); - Assert.assertTrue(timing.awaitLatch(latch)); - } - - @Test - public void testGetChildren() - { - TestModel model = new TestModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1)); - ModeledCuratorFramework<TestModel> client = ModeledCuratorFramework.builder(rawClient, modelSpec).build(); - complete(client.at("one").set(model)); - complete(client.at("two").set(model)); - complete(client.at("three").set(model)); - - Set<ZPath> expected = Sets.newHashSet(path.at("one"), path.at("two"), path.at("three")); - complete(client.children(), (children, e) -> Assert.assertEquals(Sets.newHashSet(children), expected)); - } -}
http://git-wip-us.apache.org/repos/asf/curator/blob/1fcb63a5/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java new file mode 100644 index 0000000..acca126 --- /dev/null +++ b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/TestModeledFramework.java @@ -0,0 +1,122 @@ +/** + * 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.curator.x.async.modeled; + +import com.google.common.collect.Sets; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.RetryOneTime; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.x.async.AsyncStage; +import org.apache.curator.x.async.CompletableBaseClassForTests; +import org.apache.curator.x.async.modeled.models.TestModel; +import org.apache.curator.x.async.modeled.models.TestNewerModel; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import java.math.BigInteger; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +public class TestModeledFramework extends CompletableBaseClassForTests +{ + private static final ZPath path = ZPath.parse("/test/path"); + private CuratorFramework rawClient; + private JacksonModelSerializer<TestModel> serializer; + private JacksonModelSerializer<TestNewerModel> newSerializer; + private ModelSpec<TestModel> modelSpec; + private ModelSpec<TestNewerModel> newModelSpec; + + @BeforeMethod + @Override + public void setup() throws Exception + { + super.setup(); + + rawClient = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + rawClient.start(); + + serializer = new JacksonModelSerializer<>(TestModel.class); + newSerializer = new JacksonModelSerializer<>(TestNewerModel.class); + + modelSpec = ModelSpec.builder(path, serializer).build(); + newModelSpec = ModelSpec.builder(path, newSerializer).build(); + } + + @AfterMethod + @Override + public void teardown() throws Exception + { + CloseableUtils.closeQuietly(rawClient); + super.teardown(); + } + + @Test + public void testCrud() + { + TestModel rawModel = new TestModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1)); + TestModel rawModel2 = new TestModel("Wayne", "Rooney", "Old Trafford", 10, BigInteger.valueOf(1)); + ModeledFramework<TestModel> client = ModeledFramework.wrap(rawClient, modelSpec); + AsyncStage<String> stage = client.set(rawModel); + Assert.assertNull(stage.event()); + complete(stage, (s, e) -> Assert.assertNotNull(s)); + complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel)); + complete(client.update(rawModel2)); + complete(client.read(), (model, e) -> Assert.assertEquals(model, rawModel2)); + complete(client.delete()); + complete(client.checkExists(), (stat, e) -> Assert.assertNull(stat)); + } + + @Test + public void testBackwardCompatibility() + { + TestNewerModel rawNewModel = new TestNewerModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1), 100); + ModeledFramework<TestNewerModel> clientForNew = ModeledFramework.wrap(rawClient, newModelSpec); + complete(clientForNew.set(rawNewModel), (s, e) -> Assert.assertNotNull(s)); + + ModeledFramework<TestModel> clientForOld = ModeledFramework.wrap(rawClient, modelSpec); + complete(clientForOld.read(), (model, e) -> Assert.assertTrue(rawNewModel.equalsOld(model))); + } + + @Test + public void testWatched() throws InterruptedException + { + CountDownLatch latch = new CountDownLatch(1); + ModeledFramework<TestModel> client = ModeledFramework.builder(rawClient, modelSpec).watched().build(); + client.checkExists().event().whenComplete((event, ex) -> latch.countDown()); + timing.sleepABit(); + Assert.assertEquals(latch.getCount(), 1); + client.set(new TestModel()); + Assert.assertTrue(timing.awaitLatch(latch)); + } + + @Test + public void testGetChildren() + { + TestModel model = new TestModel("John", "Galt", "1 Galt's Gulch", 42, BigInteger.valueOf(1)); + ModeledFramework<TestModel> client = ModeledFramework.builder(rawClient, modelSpec).build(); + complete(client.at("one").set(model)); + complete(client.at("two").set(model)); + complete(client.at("three").set(model)); + + Set<ZPath> expected = Sets.newHashSet(path.at("one"), path.at("two"), path.at("three")); + complete(client.children(), (children, e) -> Assert.assertEquals(Sets.newHashSet(children), expected)); + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/1fcb63a5/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledCuratorFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledCuratorFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledCuratorFramework.java deleted file mode 100644 index 1bc3434..0000000 --- a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledCuratorFramework.java +++ /dev/null @@ -1,96 +0,0 @@ -/** - * 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.curator.x.async.modeled.details; - -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.retry.RetryOneTime; -import org.apache.curator.utils.CloseableUtils; -import org.apache.curator.x.async.CompletableBaseClassForTests; -import org.apache.curator.x.async.modeled.cached.CachedModeledCuratorFramework; -import org.apache.curator.x.async.modeled.ModelSpec; -import org.apache.curator.x.async.modeled.JacksonModelSerializer; -import org.apache.curator.x.async.modeled.ModelSerializer; -import org.apache.curator.x.async.modeled.ModeledCuratorFramework; -import org.apache.curator.x.async.modeled.ZPath; -import org.apache.curator.x.async.modeled.models.TestSimpleModel; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import java.util.concurrent.atomic.AtomicInteger; - -public class TestCachedModeledCuratorFramework extends CompletableBaseClassForTests -{ - private static final ZPath path = ZPath.parse("/test/path"); - private CuratorFramework rawClient; - private CachedModeledCuratorFramework<TestSimpleModel> client; - - @BeforeMethod - @Override - public void setup() throws Exception - { - super.setup(); - - rawClient = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); - rawClient.start(); - - ModelSerializer<TestSimpleModel> serializer = new JacksonModelSerializer<>(TestSimpleModel.class); - client = ModeledCuratorFramework.builder(rawClient, ModelSpec.builder(path, serializer).build()).build().cached(); - } - - @AfterMethod - @Override - public void teardown() throws Exception - { - CloseableUtils.closeQuietly(rawClient); - super.teardown(); - } - - @Test - public void testBasic() throws InterruptedException - { - client.start(); - - AtomicInteger counter = new AtomicInteger(); -// ((CachedModeledCuratorFrameworkImpl)client).debugCachedReadCount = counter; - - complete(client.read()); - Assert.assertEquals(counter.get(), 0); - - complete(client.set(new TestSimpleModel("test", 10))); - Assert.assertEquals(counter.get(), 0); - - timing.sleepABit(); - - complete(client.read()); - Assert.assertEquals(counter.get(), 1); - counter.set(0); - - complete(client.set(new TestSimpleModel("test2", 20))); - Assert.assertEquals(counter.get(), 0); - - timing.sleepABit(); - - complete(client.read(), (model, e) -> Assert.assertEquals(model, new TestSimpleModel("test2", 20))); - Assert.assertEquals(counter.get(), 1); - - client.close(); - } -} http://git-wip-us.apache.org/repos/asf/curator/blob/1fcb63a5/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledFramework.java ---------------------------------------------------------------------- diff --git a/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledFramework.java b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledFramework.java new file mode 100644 index 0000000..70ef93e --- /dev/null +++ b/curator-x-async/src/test/java/org/apache/curator/x/async/modeled/details/TestCachedModeledFramework.java @@ -0,0 +1,96 @@ +/** + * 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.curator.x.async.modeled.details; + +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.RetryOneTime; +import org.apache.curator.utils.CloseableUtils; +import org.apache.curator.x.async.CompletableBaseClassForTests; +import org.apache.curator.x.async.modeled.cached.CachedModeledFramework; +import org.apache.curator.x.async.modeled.ModelSpec; +import org.apache.curator.x.async.modeled.JacksonModelSerializer; +import org.apache.curator.x.async.modeled.ModelSerializer; +import org.apache.curator.x.async.modeled.ModeledFramework; +import org.apache.curator.x.async.modeled.ZPath; +import org.apache.curator.x.async.modeled.models.TestSimpleModel; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import java.util.concurrent.atomic.AtomicInteger; + +public class TestCachedModeledFramework extends CompletableBaseClassForTests +{ + private static final ZPath path = ZPath.parse("/test/path"); + private CuratorFramework rawClient; + private CachedModeledFramework<TestSimpleModel> client; + + @BeforeMethod + @Override + public void setup() throws Exception + { + super.setup(); + + rawClient = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); + rawClient.start(); + + ModelSerializer<TestSimpleModel> serializer = new JacksonModelSerializer<>(TestSimpleModel.class); + client = ModeledFramework.builder(rawClient, ModelSpec.builder(path, serializer).build()).build().cached(); + } + + @AfterMethod + @Override + public void teardown() throws Exception + { + CloseableUtils.closeQuietly(rawClient); + super.teardown(); + } + + @Test + public void testBasic() throws InterruptedException + { + client.start(); + + AtomicInteger counter = new AtomicInteger(); +// ((CachedModeledCuratorFrameworkImpl)client).debugCachedReadCount = counter; + + complete(client.read()); + Assert.assertEquals(counter.get(), 0); + + complete(client.set(new TestSimpleModel("test", 10))); + Assert.assertEquals(counter.get(), 0); + + timing.sleepABit(); + + complete(client.read()); + Assert.assertEquals(counter.get(), 1); + counter.set(0); + + complete(client.set(new TestSimpleModel("test2", 20))); + Assert.assertEquals(counter.get(), 0); + + timing.sleepABit(); + + complete(client.read(), (model, e) -> Assert.assertEquals(model, new TestSimpleModel("test2", 20))); + Assert.assertEquals(counter.get(), 1); + + client.close(); + } +}
