Skip to content

Commit 90dbaa4

Browse files
committed
fixed issue arangodb#7: clear call stack after executing batch request
1 parent 944698a commit 90dbaa4

File tree

4 files changed

+79
-13
lines changed

4 files changed

+79
-13
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public void startBatchMode() throws ArangoException {
202202
if (this.httpManager.isBatchModeActive()) {
203203
throw new ArangoException("BatchMode is already active.");
204204
}
205+
this.httpManager.emptyCallStack();
205206
this.httpManager.setBatchModeActive(true);
206207
this.createModuleDrivers(true);
207208

@@ -414,15 +415,21 @@ public <T> T getBatchResponseByRequestId(String requestId) throws ArangoExceptio
414415
requestId);
415416
try {
416417
this.httpManager.setPreDefinedResponse(batchResponseEntity.getHttpResponseEntity());
417-
return (T) batchResponseEntity
418+
419+
T result = (T) batchResponseEntity
418420
.getInvocationObject()
419421
.getMethod()
420422
.invoke(
421423
batchResponseEntity.getInvocationObject().getArangoDriver(),
422424
batchResponseEntity.getInvocationObject().getArgs());
425+
this.httpManager.setPreDefinedResponse(null);
426+
return result;
423427
} catch (InvocationTargetException e) {
424-
return (T) createEntity(batchResponseEntity.getHttpResponseEntity(), (Class) DefaultEntity.class);
428+
T result = (T) createEntity(batchResponseEntity.getHttpResponseEntity(), (Class) DefaultEntity.class);
429+
this.httpManager.setPreDefinedResponse(null);
430+
return result;
425431
} catch (Exception e) {
432+
this.httpManager.setPreDefinedResponse(null);
426433
throw new ArangoException(e);
427434
}
428435
}
@@ -441,6 +448,8 @@ public void cancelBatchMode() throws ArangoException {
441448
}
442449
this.httpManager.setBatchModeActive(false);
443450
this.createModuleDrivers(false);
451+
this.httpManager.emptyCallStack();
452+
this.httpManager.setPreDefinedResponse(null);
444453
}
445454

446455
/**

src/main/java/com/arangodb/http/BatchHttpManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ public boolean isBatchModeActive() {
6666
public void setBatchModeActive(boolean batchModeActive) {
6767
this.batchModeActive = batchModeActive;
6868
}
69+
70+
public void emptyCallStack() {
71+
this.callStack = new ArrayList<BatchPart>();
72+
}
6973
}

src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public List<String> getDocuments(String database, String collectionName, boolean
123123
createEndpointUrl(baseUrl, database, "/_api/document"),
124124
new MapBuilder("collection", collectionName).get()
125125
);
126-
126+
127127
DocumentsEntity entity = createEntity(res, DocumentsEntity.class);
128128
List<String> documents = CollectionUtils.safety(entity.getDocuments());
129129

src/test/java/com/arangodb/ArangoDriverBatchTest.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.junit.After;
2020
import org.junit.Before;
21+
import org.junit.Ignore;
2122
import org.junit.Test;
2223

2324
import com.arangodb.ArangoConfigure;
@@ -37,6 +38,8 @@
3738
*/
3839
public class ArangoDriverBatchTest extends BaseTest {
3940

41+
String colName = "unit_test_batchTest";
42+
4043
public ArangoDriverBatchTest(ArangoConfigure configure, ArangoDriver driver) {
4144
super(configure, driver);
4245
}
@@ -45,16 +48,26 @@ public ArangoDriverBatchTest(ArangoConfigure configure, ArangoDriver driver) {
4548

4649
@Before
4750
public void before() throws ArangoException {
48-
for (String col: new String[]{"blub"}) {
49-
try {
50-
driver.deleteCollection(col);
51-
} catch (ArangoException e) {
52-
}
51+
try {
52+
driver.cancelBatchMode();
53+
} catch (ArangoException e) {
54+
}
55+
try {
56+
driver.deleteCollection(colName);
57+
} catch (ArangoException e) {
5358
}
5459
}
5560

5661
@After
5762
public void after() {
63+
try {
64+
driver.cancelBatchMode();
65+
} catch (ArangoException e) {
66+
}
67+
try {
68+
driver.deleteCollection(colName);
69+
} catch (ArangoException e) {
70+
}
5871
}
5972

6073
@Test
@@ -86,14 +99,17 @@ public void test_StartCancelExecuteBatchMode() throws ArangoException {
8699
}
87100
assertThat(msg , is("BatchMode is not active."));
88101

89-
90102
}
91103

92-
93-
94104
@Test
95105
public void test_execBatchMode() throws ArangoException {
96106

107+
try {
108+
driver.truncateCollection("_aqlfunctions");
109+
} catch (Exception e) {
110+
e.printStackTrace();
111+
}
112+
97113
driver.startBatchMode();
98114

99115
BaseEntity res = driver.createAqlFunction(
@@ -116,13 +132,13 @@ public void test_execBatchMode() throws ArangoException {
116132

117133
for (int i = 0; i < 10; i++) {
118134
TestComplexEntity01 value = new TestComplexEntity01("user-" + i, "data:" + i, i);
119-
res = driver.createDocument("blub", value, true, false);
135+
res = driver.createDocument(colName, value, true, false);
120136

121137
assertThat(res.getStatusCode(), is(206));
122138
assertThat(res.getRequestId(), is("request" + (4 + i)));
123139
}
124140

125-
List<String> r = driver.getDocuments("blub");
141+
List<String> r = driver.getDocuments(colName);
126142

127143
DefaultEntity result = driver.executeBatch();
128144
DefaultEntity created = driver.getBatchResponseByRequestId("request1");
@@ -138,6 +154,43 @@ public void test_execBatchMode() throws ArangoException {
138154
List<String> documents = driver.getBatchResponseByRequestId("request14");
139155
assertThat(documents.size(), is(10));
140156

157+
try {
158+
driver.truncateCollection("_aqlfunctions");
159+
} catch (Exception e) {
160+
e.printStackTrace();
161+
}
162+
163+
}
164+
165+
@Test
166+
public void test_execBatchMode_twice() throws ArangoException {
167+
168+
driver.startBatchMode();
169+
170+
BaseEntity res;
171+
172+
for (int i = 0; i < 10; i++) {
173+
TestComplexEntity01 value = new TestComplexEntity01("user-" + i, "data:" + i, i);
174+
res = driver.createDocument(colName, value, true, false);
175+
assertThat(res.getRequestId(), is("request" + (i + 1)));
176+
}
177+
178+
driver.executeBatch();
179+
180+
assertThat(driver.getDocuments(colName).size(), is(10));
181+
182+
driver.startBatchMode();
183+
184+
for (int i = 20; i < 30; i++) {
185+
TestComplexEntity01 value = new TestComplexEntity01("user-" + i, "data:" + i, i);
186+
res = driver.createDocument(colName, value, true, false);
187+
assertThat(res.getRequestId(), is("request" + (i + 1 - 20)));
188+
}
189+
190+
driver.executeBatch();
191+
192+
assertThat(driver.getDocuments(colName).size(), is(20));
193+
141194
}
142195

143196
}

0 commit comments

Comments
 (0)