From cbabebc84b0de487db4349b790c45f18230d0281 Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 4 Mar 2016 10:44:11 -0800 Subject: [PATCH] Reduced number of threads. Couple test adjustments. --- .../gcloud/dns/testing/LocalDnsHelper.java | 14 +- .../dns/testing/LocalDnsHelperTest.java | 129 +++++++++--------- 2 files changed, 73 insertions(+), 70 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java index cafbf764db69..f9cd1a11281e 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java @@ -110,10 +110,10 @@ public class LocalDnsHelper { ImmutableList.of("google.com.", "com.", "example.com.", "net.", "org.")); private static final Pattern ZONE_NAME_RE = Pattern.compile("[a-z][a-z0-9-]*"); private static final ScheduledExecutorService EXECUTORS = - Executors.newScheduledThreadPool(4, Executors.defaultThreadFactory()); + Executors.newScheduledThreadPool(2, Executors.defaultThreadFactory()); + private static final String PROJECT_ID = "dummyprojectid"; static { - try { BASE_CONTEXT = new URI(CONTEXT); } catch (URISyntaxException e) { @@ -125,7 +125,6 @@ public class LocalDnsHelper { private long delayChange; private final HttpServer server; private final int port; - private String projectId; /** * For matching URLs to operations. @@ -364,9 +363,8 @@ private Response handleZoneCreate(HttpExchange exchange, String projectId, Strin } } - private LocalDnsHelper(String projectId, long delay) { + private LocalDnsHelper(long delay) { this.delayChange = delay; - this.projectId = projectId; try { server = HttpServer.create(new InetSocketAddress(0), 0); port = server.getAddress().getPort(); @@ -391,15 +389,15 @@ ConcurrentSkipListMap projects() { * * @param delay delay for processing changes in ms or 0 for synchronous processing */ - public static LocalDnsHelper create(String projectId, Long delay) { - return new LocalDnsHelper(projectId, delay); + public static LocalDnsHelper create(Long delay) { + return new LocalDnsHelper(delay); } /** * Returns a {@link DnsOptions} instance that sets the host to use the mock server. */ public DnsOptions options() { - return DnsOptions.builder().projectId(projectId).host("http://localhost:" + port).build(); + return DnsOptions.builder().projectId(PROJECT_ID).host("http://localhost:" + port).build(); } /** diff --git a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java index 5c8fb3bd8d3e..75d8b005ed55 100644 --- a/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java +++ b/gcloud-java-dns/src/test/java/com/google/gcloud/dns/testing/LocalDnsHelperTest.java @@ -38,7 +38,9 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.Timeout; import java.util.HashMap; import java.util.HashSet; @@ -64,11 +66,10 @@ public class LocalDnsHelperTest { private static final Change CHANGE2 = new Change(); private static final Change CHANGE_KEEP = new Change(); private static final Change CHANGE_COMPLEX = new Change(); - private static final String REAL_PROJECT_ID = "someprojectid"; - private static final LocalDnsHelper LOCAL_DNS_HELPER = LocalDnsHelper.create(REAL_PROJECT_ID, 0L); + private static final LocalDnsHelper LOCAL_DNS_HELPER = LocalDnsHelper.create(0L); private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); - private static final DnsRpc RPC = new DefaultDnsRpc(LOCAL_DNS_HELPER.options().toBuilder() - .projectId(REAL_PROJECT_ID).build()); + private static final DnsRpc RPC = new DefaultDnsRpc(LOCAL_DNS_HELPER.options()); + private static final String REAL_PROJECT_ID = LOCAL_DNS_HELPER.options().projectId(); private Map optionsMap; @BeforeClass @@ -98,6 +99,9 @@ public static void before() { LOCAL_DNS_HELPER.start(); } + @Rule + public Timeout globalTimeout = Timeout.seconds(60); + @Before public void setUp() { resetProjects(); @@ -122,60 +126,6 @@ private static void assertEqChangesIgnoreStatus(Change expected, Change actual) assertEquals(expected.getStartTime(), actual.getStartTime()); } - private static void waitUntilComplete(DnsRpc rpc, String zoneName, String changeId) { - while (true) { - Change change = rpc.getChangeRequest(zoneName, changeId, EMPTY_RPC_OPTIONS); - if ("done".equals(change.getStatus())) { - return; - } - try { - Thread.sleep(50L); - } catch (InterruptedException e) { - fail("Thread was interrupted while waiting for change processing."); - } - } - } - - private static void executeCreateAndApplyChangeTest(DnsRpc rpc) { - rpc.create(ZONE1, EMPTY_RPC_OPTIONS); - assertNull(rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS)); - // add - Change createdChange = rpc.applyChangeRequest(ZONE1.getName(), CHANGE1, EMPTY_RPC_OPTIONS); - assertEquals(createdChange.getAdditions(), CHANGE1.getAdditions()); - assertEquals(createdChange.getDeletions(), CHANGE1.getDeletions()); - assertNotNull(createdChange.getStartTime()); - assertEquals("1", createdChange.getId()); - Change retrievedChange = rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS); - assertEqChangesIgnoreStatus(createdChange, retrievedChange); - assertNull(rpc.getChangeRequest(ZONE1.getName(), "2", EMPTY_RPC_OPTIONS)); - waitUntilComplete(rpc, ZONE1.getName(), "1"); // necessary for the following to return 409 - try { - rpc.applyChangeRequest(ZONE1.getName(), CHANGE1, EMPTY_RPC_OPTIONS); - fail(); - } catch (DnsException ex) { - assertEquals(409, ex.code()); - } - assertNotNull(rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS)); - assertNull(rpc.getChangeRequest(ZONE1.getName(), "2", EMPTY_RPC_OPTIONS)); - // delete - rpc.applyChangeRequest(ZONE1.getName(), CHANGE2, EMPTY_RPC_OPTIONS); - assertNotNull(rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS)); - assertNotNull(rpc.getChangeRequest(ZONE1.getName(), "2", EMPTY_RPC_OPTIONS)); - waitUntilComplete(rpc, ZONE1.getName(), "2"); - rpc.applyChangeRequest(ZONE1.getName(), CHANGE_KEEP, EMPTY_RPC_OPTIONS); - waitUntilComplete(rpc, ZONE1.getName(), "3"); - Iterable results = - rpc.listDnsRecords(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); - boolean ok = false; - for (ResourceRecordSet dnsRecord : results) { - if (dnsRecord.getName().equals(RRSET_KEEP.getName()) - && dnsRecord.getType().equals(RRSET_KEEP.getType())) { - ok = true; - } - } - assertTrue(ok); - } - @Test public void testCreateZone() { ManagedZone created = RPC.create(ZONE1, EMPTY_RPC_OPTIONS); @@ -397,14 +347,70 @@ public void testCreateAndApplyChange() { @Test public void testCreateAndApplyChangeWithThreads() { - LocalDnsHelper localDnsThreaded = LocalDnsHelper.create(REAL_PROJECT_ID, 50L); + LocalDnsHelper localDnsThreaded = LocalDnsHelper.create(50L); localDnsThreaded.start(); - DnsRpc rpc = new DefaultDnsRpc(localDnsThreaded.options().toBuilder() - .projectId(REAL_PROJECT_ID).build()); + DnsRpc rpc = new DefaultDnsRpc(localDnsThreaded.options()); executeCreateAndApplyChangeTest(rpc); localDnsThreaded.stop(); } + private static void waitForChangeToComplete(DnsRpc rpc, String zoneName, String changeId) { + while (true) { + Change change = rpc.getChangeRequest(zoneName, changeId, EMPTY_RPC_OPTIONS); + if ("done".equals(change.getStatus())) { + return; + } + try { + Thread.sleep(50L); + } catch (InterruptedException e) { + fail("Thread was interrupted while waiting for change processing."); + } + } + } + + private static void executeCreateAndApplyChangeTest(DnsRpc rpc) { + rpc.create(ZONE1, EMPTY_RPC_OPTIONS); + assertNull(rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS)); + // add + Change createdChange = rpc.applyChangeRequest(ZONE1.getName(), CHANGE1, EMPTY_RPC_OPTIONS); + assertEquals(CHANGE1.getAdditions(), createdChange.getAdditions()); + assertEquals(CHANGE1.getDeletions(), createdChange.getDeletions()); + assertNotNull(createdChange.getStartTime()); + assertEquals("1", createdChange.getId()); + waitForChangeToComplete(rpc, ZONE1.getName(), "1"); // necessary for the following to return 409 + try { + rpc.applyChangeRequest(ZONE1.getName(), CHANGE1, EMPTY_RPC_OPTIONS); + fail(); + } catch (DnsException ex) { + assertEquals(409, ex.code()); + assertTrue(ex.getMessage().contains("already exists")); + } + assertNotNull(rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS)); + assertNull(rpc.getChangeRequest(ZONE1.getName(), "2", EMPTY_RPC_OPTIONS)); + // delete + rpc.applyChangeRequest(ZONE1.getName(), CHANGE2, EMPTY_RPC_OPTIONS); + assertNotNull(rpc.getChangeRequest(ZONE1.getName(), "1", EMPTY_RPC_OPTIONS)); + assertNotNull(rpc.getChangeRequest(ZONE1.getName(), "2", EMPTY_RPC_OPTIONS)); + waitForChangeToComplete(rpc, ZONE1.getName(), "2"); + rpc.applyChangeRequest(ZONE1.getName(), CHANGE_KEEP, EMPTY_RPC_OPTIONS); + waitForChangeToComplete(rpc, ZONE1.getName(), "3"); + Iterable results = + rpc.listDnsRecords(ZONE1.getName(), EMPTY_RPC_OPTIONS).results(); + boolean rrsetKeep = false; + boolean rrset1 = false; + for (ResourceRecordSet dnsRecord : results) { + if (dnsRecord.getName().equals(RRSET_KEEP.getName()) + && dnsRecord.getType().equals(RRSET_KEEP.getType())) { + rrsetKeep = true; + } else if (dnsRecord.getName().equals(RRSET1.getName()) + && dnsRecord.getType().equals(RRSET1.getType())) { + rrset1 = true; + } + } + assertTrue(rrset1); + assertTrue(rrsetKeep); + } + @Test public void testGetProject() { // the projects are automatically created when getProject is called @@ -1332,7 +1338,6 @@ public void testCreateChangeContentValidation() { Change validChange = new Change(); validChange.setAdditions(ImmutableList.of(validA)); LOCAL_DNS_HELPER.createZone(PROJECT_ID1, ZONE1); - LOCAL_DNS_HELPER.createChange(PROJECT_ID1, ZONE_NAME1, validChange); LocalDnsHelper.Response response = LOCAL_DNS_HELPER.createChange(PROJECT_ID1, ZONE_NAME1, validChange);