Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created August 10, 2012 14:11
Show Gist options
  • Save ingenthr/3314476 to your computer and use it in GitHub Desktop.
Save ingenthr/3314476 to your computer and use it in GitHub Desktop.
diff of changes to add error handling and content-type
$ git show 32ae6c458ae10372954b55b83dfb6f23895ccc1a | cat
commit 32ae6c458ae10372954b55b83dfb6f23895ccc1a
Author: Matt Ingenthron <[email protected]>
Date: Thu Jul 26 22:55:49 2012 -0700
WIP: fixed adding views in tests, now trying to make tests pass
Change-Id: I29e6c9894a6bc83b0a1ad5758d7ac609fcfe28b4
diff --git a/build.xml b/build.xml
index bea6016..ddb095c 100644
--- a/build.xml
+++ b/build.xml
@@ -237,14 +237,15 @@
<echo>Test Type (unit|ci) ${test.type}</echo>
<delete dir="${junit.dir}"/>
<mkdir dir="${junit.dir}/data"/>
- <junit fork="yes" timeout="60000" failureproperty="junit.failure">
+ <junit fork="yes" timeout="60000" failureproperty="junit.failure" enableTestListenerEvents="true">
<batchtest todir="${junit.dir}/data" fork="yes">
<fileset dir="${test.dir}">
- <include name="**/*Test.java"/>
+ <include name="**/*ViewTest.java"/>
</fileset>
<formatter type="xml"/>
</batchtest>
- <jvmarg value="-ea" />
+ <!-- jvmarg value="-ea" / -->
+ <jvmarg line="-ea -agentlib:jdwp=transport=dt_socket,address=localhost:8000,server=y,suspend=y" />
<sysproperty key="server.address_v4" value="${server.address_v4}"/>
<sysproperty key="server.address_v6" value="${server.address_v6}"/>
<sysproperty key="server.type" value="${server.type}"/>
diff --git a/src/main/java/com/couchbase/client/CouchbaseClient.java b/src/main/java/com/couchbase/client/CouchbaseClient.java
index 8079640..a83fe53 100644
--- a/src/main/java/com/couchbase/client/CouchbaseClient.java
+++ b/src/main/java/com/couchbase/client/CouchbaseClient.java
@@ -294,6 +294,7 @@ public class CouchbaseClient extends MemcachedClient
});
crv.setOperation(op);
addOp(op);
+ assert crv != null : "Problem retrieving view";
return crv;
}
@@ -411,7 +412,14 @@ public class CouchbaseClient extends MemcachedClient
*/
private HttpFuture<ViewResponse> asyncQueryAndIncludeDocs(View view,
Query query) {
- String uri = view.getURI() + query.toString();
+ assert view != null : "Who passed me a null view";
+ assert query != null : "who passed me a null query";
+ String viewUri = view.getURI();
+ String queryToRun = query.toString();
+ assert viewUri != null : "view URI seems to be null";
+ assert queryToRun != null : "query seems to be null";
+ String uri = viewUri + queryToRun;
+ getLogger().info("lookin for:" + uri);
final CountDownLatch couchLatch = new CountDownLatch(1);
final ViewFuture crv = new ViewFuture(couchLatch, 60000);
diff --git a/src/test/java/com/couchbase/client/TestOperationPutImpl.java b/src/test/java/com/couchbase/client/TestOperationPutImpl.java
new file mode 100644
index 0000000..649d0a3
--- /dev/null
+++ b/src/test/java/com/couchbase/client/TestOperationPutImpl.java
@@ -0,0 +1,79 @@
+/**
+ * Copyright (C) 2009-2011 Couchbase, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
+ * IN THE SOFTWARE.
+ */
+
+package com.couchbase.client;
+
+import com.couchbase.client.protocol.views.HttpOperationImpl;
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.net.HttpURLConnection;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.spy.memcached.ops.OperationStatus;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+
+/**
+ * A TestOperationImpl.
+ */
+public class TestOperationPutImpl extends HttpOperationImpl implements
+ TestOperation {
+
+ public TestOperationPutImpl(HttpRequest r, TestCallback testCallback) {
+ super(r, testCallback);
+ }
+
+ @Override
+ public void handleResponse(HttpResponse response) {
+ StringBuilder json = new StringBuilder(""); // workaround for null returns
+ json.append(getEntityString(response));
+ int errorcode = response.getStatusLine().getStatusCode();
+ // read the response into a string
+ InputStream bi;
+ StringBuffer responseContent = new StringBuffer("");
+ try {
+ bi = response.getEntity().getContent();
+ byte[] buffer = new byte[bi.available() ];
+ int bytesRead = bi.read(buffer);
+ responseContent.append(new String(buffer));
+ } catch (IOException ex) {
+ Logger.getLogger(TestOperationImpl.class.getName()).log(Level.SEVERE, "Could not read test response.", ex);
+ } catch (IllegalStateException ex) {
+ Logger.getLogger(TestOperationImpl.class.getName()).log(Level.SEVERE, null, ex);
+ }
+
+
+
+ if (errorcode == HttpURLConnection.HTTP_CREATED) {
+ ((TestCallback) callback).getData(json.toString());
+ callback.receivedStatus(new OperationStatus(true, "OK"));
+ } else {
+ callback.receivedStatus(new OperationStatus(false,
+ Integer.toString(errorcode) + ": " + responseContent));
+ }
+ callback.complete();
+ }
+
+}
diff --git a/src/test/java/com/couchbase/client/TestingClient.java b/src/test/java/com/couchbase/client/TestingClient.java
index c8ab5e5..95ac3b6 100644
--- a/src/test/java/com/couchbase/client/TestingClient.java
+++ b/src/test/java/com/couchbase/client/TestingClient.java
@@ -37,6 +37,7 @@ import net.spy.memcached.ops.OperationStatus;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest;
@@ -59,13 +60,16 @@ public class TestingClient extends CouchbaseClient {
HttpRequest request = new BasicHttpEntityEnclosingRequest("PUT", uri,
HttpVersion.HTTP_1_1);
+ request.setHeader(new BasicHeader("Content-Type", "application/json"));
StringEntity entity = new StringEntity(document);
((BasicHttpEntityEnclosingRequest) request).setEntity(entity);
- HttpOperationImpl op = new TestOperationImpl(request, new TestCallback() {
+ HttpOperationImpl op = new TestOperationPutImpl(request, new TestCallback() {
private String json;
@Override
public void receivedStatus(OperationStatus status) {
+// System.err.println("<>><<><><><><><> Hey, who is calling me here: ");
+// Thread.dumpStack();
crv.set(json, status);
}
diff --git a/src/test/java/com/couchbase/client/ViewTest.java b/src/test/java/com/couchbase/client/ViewTest.java
index 4ad8c79..c11014d 100644
--- a/src/test/java/com/couchbase/client/ViewTest.java
+++ b/src/test/java/com/couchbase/client/ViewTest.java
@@ -122,9 +122,17 @@ public class ViewTest {
assert c.set(item.getKey(), 0,
(String) item.getValue()).get().booleanValue();
}
- c.asyncHttpPut(docUri, view);
+ HttpFuture<String> asyncHttpPut = c.asyncHttpPut(docUri, view);
+ String response = asyncHttpPut.get();
+ OperationStatus status = asyncHttpPut.getStatus();
+ System.err.println("<><><><><><><>Operation Status is: " + status);
+ if (!status.isSuccess()) {
+ System.err.println("<><><><><><><>Operation Status is: " + status);
+ assert false : "Could not load views: " + status.getMessage() + " with response " + response;
+ }
c.shutdown();
- Thread.sleep(15000);
+ System.out.println("Setup of design docs complete, sleeping until they propogate.");
+ Thread.sleep(5000);
}
@Before
@@ -183,6 +191,7 @@ public class ViewTest {
query.setIncludeDocs(true);
query.setStale(Stale.FALSE);
View view = client.getView(DESIGN_DOC_W_REDUCE, VIEW_NAME_W_REDUCE);
+ assert view != null : "Could not retrieve view";
HttpFuture<ViewResponse> future = client.asyncQuery(view, query);
ViewResponse response=null;
try {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment