Skip to content

Commit 604596d

Browse files
committed
Refactor run methods into a more generic TestHelper class in the async driver
1 parent 1f2df2b commit 604596d

File tree

6 files changed

+123
-130
lines changed

6 files changed

+123
-130
lines changed

driver-async/src/test/functional/com/mongodb/async/client/SmokeTestSpecification.groovy

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
package com.mongodb.async.client
1818

1919
import com.mongodb.MongoNamespace
20-
import com.mongodb.async.FutureResultCallback
2120
import org.bson.Document
2221
import spock.lang.IgnoreIf
2322

2423
import static com.mongodb.ClusterFixture.serverVersionAtLeast
2524
import static com.mongodb.async.client.Fixture.getMongoClient
2625
import static com.mongodb.async.client.Fixture.isSharded
26+
import static com.mongodb.async.client.TestHelper.run
2727
import static java.util.Arrays.asList
28-
import static java.util.concurrent.TimeUnit.SECONDS
2928

3029
class SmokeTestSpecification extends FunctionalSpecification {
3130

@@ -179,12 +178,4 @@ class SmokeTestSpecification extends FunctionalSpecification {
179178
!run(database.listCollectionNames().&into, []).contains(collectionName)
180179
run(database.listCollectionNames().&into, []).contains(newCollectionName)
181180
}
182-
183-
def run(operation, ... args) {
184-
def futureResultCallback = new FutureResultCallback()
185-
def opArgs = (args != null) ? args : []
186-
operation.call(*opArgs + futureResultCallback)
187-
futureResultCallback.get(60, SECONDS)
188-
}
189-
190181
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2016 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package com.mongodb.async.client
19+
20+
import com.mongodb.async.FutureResultCallback
21+
22+
import java.util.concurrent.TimeUnit
23+
24+
class TestHelper {
25+
static run(operation, ... args) {
26+
runOp(operation, 60, *args)
27+
}
28+
29+
static runSlow(operation, ... args) {
30+
runOp(operation, 180, *args)
31+
}
32+
33+
static runOp(operation, timeout, ... args) {
34+
FutureResultCallback futureResultCallback = new FutureResultCallback()
35+
List opArgs = (args != null) ? args : []
36+
operation.call(*opArgs + futureResultCallback)
37+
futureResultCallback.get(timeout, TimeUnit.SECONDS)
38+
}
39+
}

driver-async/src/test/functional/com/mongodb/async/client/gridfs/GridFSBucketSmokeTestSpecification.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ import java.nio.ByteBuffer
3737
import java.security.MessageDigest
3838
import java.security.SecureRandom
3939

40-
import static GridFSTestHelper.run
4140
import static com.mongodb.async.client.Fixture.getDefaultDatabaseName
4241
import static com.mongodb.async.client.Fixture.getMongoClient
43-
import static com.mongodb.async.client.gridfs.GridFSTestHelper.runSlow
42+
import static com.mongodb.async.client.TestHelper.run
43+
import static com.mongodb.async.client.TestHelper.runSlow
4444
import static com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncInputStream
4545
import static com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncOutputStream
4646
import static com.mongodb.client.model.Filters.eq

driver-async/src/test/functional/com/mongodb/async/client/gridfs/GridFSTestHelper.groovy

Lines changed: 0 additions & 111 deletions
This file was deleted.

driver-async/src/test/functional/com/mongodb/async/client/gridfs/helpers/AsyncStreamHelperTestSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import java.nio.ByteBuffer
2929

3030
import static com.mongodb.async.client.Fixture.getDefaultDatabaseName
3131
import static com.mongodb.async.client.Fixture.getMongoClient
32-
import static com.mongodb.async.client.gridfs.GridFSTestHelper.run
32+
import static com.mongodb.async.client.TestHelper.run
3333
import static com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncInputStream
3434
import static com.mongodb.async.client.gridfs.helpers.AsyncStreamHelper.toAsyncOutputStream
3535

driver-async/src/test/functional/com/mongodb/async/client/gridfs/helpers/AsynchronousChannelHelperSmokeTestSpecification.groovy

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@ import com.mongodb.async.client.MongoCollection
2121
import com.mongodb.async.client.MongoDatabase
2222
import com.mongodb.async.client.gridfs.GridFSBucket
2323
import com.mongodb.async.client.gridfs.GridFSBucketImpl
24-
import com.mongodb.async.client.gridfs.GridFSTestHelper
2524
import org.bson.Document
2625
import spock.lang.Requires
2726

2827
import java.nio.ByteBuffer
29-
import java.nio.channels.* // Wildcard import to work around Java 1.6
30-
import java.nio.file.* // Wildcard import to work around Java 1.6
28+
import java.nio.channels.AsynchronousByteChannel
29+
import java.nio.channels.AsynchronousFileChannel
30+
import java.nio.channels.CompletionHandler
31+
import java.nio.file.Files
32+
33+
// Wildcard import to work around Java 1.6
34+
import java.nio.file.Path
35+
import java.nio.file.Paths
36+
import java.nio.file.StandardOpenOption
37+
import java.util.concurrent.Future
3138

32-
import static GridFSTestHelper.TestAsynchronousByteChannel
33-
import static GridFSTestHelper.run
3439
import static com.mongodb.async.client.Fixture.getDefaultDatabaseName
3540
import static com.mongodb.async.client.Fixture.getMongoClient
41+
import static com.mongodb.async.client.TestHelper.run
3642
import static com.mongodb.async.client.gridfs.helpers.AsynchronousChannelHelper.channelToInputStream
43+
44+
// Wildcard import to work around Java 1.6
3745
import static com.mongodb.async.client.gridfs.helpers.AsynchronousChannelHelper.channelToOutputStream
3846

3947
@Requires({ javaVersion >= 1.7 })
@@ -47,7 +55,7 @@ class AsynchronousChannelHelperSmokeTestSpecification extends FunctionalSpecific
4755
mongoDatabase = getMongoClient().getDatabase(getDefaultDatabaseName())
4856
filesCollection = mongoDatabase.getCollection('fs.files')
4957
chunksCollection = mongoDatabase.getCollection('fs.chunks')
50-
GridFSTestHelper.run(filesCollection.&drop)
58+
run(filesCollection.&drop)
5159
run(chunksCollection.&drop)
5260
gridFSBucket = new GridFSBucketImpl(mongoDatabase)
5361
}
@@ -109,4 +117,70 @@ class AsynchronousChannelHelperSmokeTestSpecification extends FunctionalSpecific
109117
content == asyncByteChannel.getWriteBuffer()
110118
}
111119

120+
static class TestAsynchronousByteChannel implements AsynchronousByteChannel, Closeable {
121+
private boolean closed
122+
private final readBuffer
123+
private final writeBuffer = ByteBuffer.allocate(1024)
124+
125+
TestAsynchronousByteChannel(final readBuffer) {
126+
this.readBuffer = readBuffer
127+
}
128+
129+
ByteBuffer getReadBuffer() {
130+
readBuffer.flip()
131+
}
132+
133+
ByteBuffer getWriteBuffer() {
134+
writeBuffer.flip()
135+
}
136+
137+
@Override
138+
<A> void read(final ByteBuffer dst, final A attachment, final CompletionHandler<Integer, ? super A> handler) {
139+
int transferAmount = Math.min(dst.remaining(), readBuffer.remaining());
140+
if (transferAmount == 0) {
141+
transferAmount = -1
142+
}
143+
if (transferAmount > 0) {
144+
ByteBuffer temp = readBuffer.duplicate()
145+
temp.limit(temp.position() + transferAmount)
146+
dst.put(temp)
147+
readBuffer.position(readBuffer.position() + transferAmount)
148+
}
149+
handler.completed(transferAmount, attachment)
150+
}
151+
152+
@Override
153+
Future<Integer> read(final ByteBuffer dst) {
154+
throw new UnsupportedOperationException('Not Supported')
155+
}
156+
157+
@Override
158+
<A> void write(final ByteBuffer src, final A attachment, final CompletionHandler<Integer, ? super A> handler) {
159+
int transferAmount = Math.min(src.remaining(), writeBuffer.remaining());
160+
if (transferAmount == 0) {
161+
transferAmount = -1
162+
}
163+
if (transferAmount > 0) {
164+
ByteBuffer temp = src.duplicate()
165+
temp.limit(temp.position() + transferAmount)
166+
writeBuffer.put(temp)
167+
}
168+
handler.completed(transferAmount, attachment)
169+
}
170+
171+
@Override
172+
Future<Integer> write(final ByteBuffer src) {
173+
throw new UnsupportedOperationException('Not Supported')
174+
}
175+
176+
@Override
177+
void close() throws IOException {
178+
closed = true
179+
}
180+
181+
@Override
182+
boolean isOpen() {
183+
!closed;
184+
}
185+
}
112186
}

0 commit comments

Comments
 (0)