Skip to content

Commit

Permalink
Reduced number of threads. Couple test adjustments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mderka committed Mar 4, 2016
1 parent 23d9868 commit cbabebc
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -391,15 +389,15 @@ ConcurrentSkipListMap<String, ProjectContainer> 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<DnsRpc.Option, ?> 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<String, Object> optionsMap;

@BeforeClass
Expand Down Expand Up @@ -98,6 +99,9 @@ public static void before() {
LOCAL_DNS_HELPER.start();
}

@Rule
public Timeout globalTimeout = Timeout.seconds(60);

@Before
public void setUp() {
resetProjects();
Expand All @@ -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<ResourceRecordSet> 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);
Expand Down Expand Up @@ -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<ResourceRecordSet> 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;
}

This comment has been minimized.

Copy link
@ajkannan

ajkannan Mar 4, 2016

Add an "else" that fails the test if we don't expect any other result.

}
assertTrue(rrset1);
assertTrue(rrsetKeep);
}

@Test
public void testGetProject() {
// the projects are automatically created when getProject is called
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cbabebc

Please sign in to comment.