Skip to content

Commit 8ef24cc

Browse files
author
William Delanoue
committed
refs #198: async support
1 parent 809ba10 commit 8ef24cc

15 files changed

+1945
-17
lines changed

CHANGELOG

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ release : 13 November 2013
2121
-----
2222
release: 02 November 2013
2323

24-
Apologees, it's been a long time since a release. I'm no longer using fongo at Foursquare, so I'll be looking to hand
24+
Apologees, it's been a long time since a release. I'm no longer using fongo at Foursquare, so I'll be looking to hand
2525
it over to an enthusiastic maintainer in the near future.
2626

2727
All changes below by @twillouer:

README.md

+35-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ don't want to spin up a `mongod` process.
1111
## Usage
1212
Add dependency to your project:
1313

14-
### If you use 3.X drivers
14+
### If you use 3.X drivers (async included)
1515

1616
```xml
1717
<dependency>
1818
<groupId>com.github.fakemongo</groupId>
1919
<artifactId>fongo</artifactId>
20-
<version>2.0.6</version>
20+
<version>2.1.0</version>
2121
<scope>test</scope>
2222
</dependency>
2323
```
2424

25-
[Other dependency management](http://search.maven.org/#artifactdetails|com.github.fakemongo|fongo|2.0.0)
25+
[Other dependency management](http://search.maven.org/#artifactdetails|com.github.fakemongo|fongo|2.1.0)
2626

2727
### If you use 2.X drivers (this branch *fongo-drivers-2.x*) will be deprecated soon
2828

@@ -35,7 +35,7 @@ Add dependency to your project:
3535
</dependency>
3636
```
3737

38-
[Other dependency management](http://search.maven.org/#artifactdetails|com.github.fakemongo|fongo|1.6.2)
38+
[Other dependency management](http://search.maven.org/#artifactdetails|com.github.fakemongo|fongo|1.6.5)
3939

4040

4141
*Alternatively: clone this repo and build the jar: `mvn package` then copy jar to your classpath*
@@ -76,7 +76,7 @@ It also has a "provided" dependency on sl4j-api for logging. If you don't alread
7676
<dependency>
7777
<groupId>ch.qos.logback</groupId>
7878
<artifactId>logback-classic</artifactId>
79-
<version>1.1.1</version>
79+
<version>1.1.7</version>
8080
<scope>test</scope>
8181
</dependency>
8282
```
@@ -145,6 +145,35 @@ public FongoRule fongoRule = new FongoRule(new ServerVersion(2, 6));
145145

146146
In this case, the drivers didn't handle queries with the same way.
147147

148+
## Junit (async drivers)
149+
150+
If you use JUnit in your project, you can use Rule to instantiate a `Fongo` object :
151+
152+
```java
153+
@Rule
154+
public FongoAsyncRule fongoAsyncRule = new FongoAsyncRule();
155+
```
156+
157+
If you need, you can easily switch to your real MongoDB server (on localhost for now).
158+
159+
```java
160+
@Rule
161+
public FongoAsyncRule fongoAsyncRule = new FongoAsyncRule(true);
162+
```
163+
164+
WARNING : In this case, the database WILL BE DROPPED when test is finish.
165+
So, use a random database name (e.g. UUID), BUT NOT your real database.
166+
167+
You can specify the version of the database with :
168+
169+
```java
170+
@Rule
171+
public FongoAsyncRule fongoAsyncRule = new FongoAsyncRule(new ServerVersion(2, 6));
172+
```
173+
174+
In this case, the drivers didn't handle queries with the same way.
175+
176+
148177
## Text Search Simulation
149178
Fongo simulates [text search](http://docs.mongodb.org/manual/reference/command/text/) now.
150179
The results of text search are quite similar to real, but not exactly.
@@ -201,6 +230,7 @@ your name to the patch contributors below. Please maintain the same code formatt
201230

202231
## Changelog
203232

233+
Version 2.1.0 include compatibility with 3.X async driver version.
204234
Version 2.0.0 break compatibility with 2.X driver version.
205235
Version 1.6.0 break compatibility with 2.12.X driver version.
206236

pom.xml

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</parent>
88
<groupId>com.github.fakemongo</groupId>
99
<artifactId>fongo</artifactId>
10-
<version>2.0.13-SNAPSHOT</version>
10+
<version>2.1.0-SNAPSHOT</version>
1111
<name>fongo</name>
1212
<description>Fake in-memory mongo</description>
1313
<url>https://github.com/fakemongo/fongo</url>
@@ -73,7 +73,13 @@
7373
<dependency>
7474
<groupId>org.mongodb</groupId>
7575
<artifactId>mongo-java-driver</artifactId>
76-
<version>3.4.1</version>
76+
<version>3.4.2</version>
77+
<scope>provided</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.mongodb</groupId>
81+
<artifactId>mongodb-driver-async</artifactId>
82+
<version>3.4.2</version>
7783
<scope>provided</scope>
7884
</dependency>
7985
<dependency>
@@ -151,7 +157,7 @@
151157
<dependency>
152158
<groupId>com.google.guava</groupId>
153159
<artifactId>guava</artifactId>
154-
<version>19.0</version>
160+
<version>21.0</version>
155161
<scope>test</scope>
156162
</dependency>
157163

@@ -183,13 +189,13 @@
183189
<dependency>
184190
<groupId>org.openjdk.jmh</groupId>
185191
<artifactId>jmh-core</artifactId>
186-
<version>1.12</version>
192+
<version>1.17.4</version>
187193
<scope>test</scope>
188194
</dependency>
189195
<dependency>
190196
<groupId>org.openjdk.jmh</groupId>
191197
<artifactId>jmh-generator-annprocess</artifactId>
192-
<version>1.12</version>
198+
<version>1.17.4</version>
193199
<scope>test</scope>
194200
</dependency>
195201
<dependency>
@@ -277,7 +283,7 @@
277283
<plugin>
278284
<groupId>net.alchim31.maven</groupId>
279285
<artifactId>scala-maven-plugin</artifactId>
280-
<version>3.2.0</version>
286+
<version>3.2.2</version>
281287
<executions>
282288
<execution>
283289
<goals>
@@ -330,7 +336,7 @@
330336
<plugin>
331337
<groupId>org.apache.maven.plugins</groupId>
332338
<artifactId>maven-javadoc-plugin</artifactId>
333-
<version>2.10.2</version>
339+
<version>2.10.3</version>
334340
<configuration>
335341
<excludePackageNames>com.mongodb:com.github.fakemongo.impl</excludePackageNames>
336342
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.fakemongo;
2+
3+
import com.mongodb.async.SingleResultCallback;
4+
import java.util.concurrent.CountDownLatch;
5+
import java.util.concurrent.TimeUnit;
6+
7+
public class AwaitResultSingleResultCallback<T> implements SingleResultCallback<T> {
8+
9+
private T result;
10+
private Throwable throwable;
11+
private CountDownLatch countDownLatch = new CountDownLatch(1);
12+
13+
@Override
14+
public void onResult(T result, Throwable t) {
15+
this.result = result;
16+
throwable = t;
17+
countDownLatch.countDown();
18+
}
19+
20+
public T awaitResult() throws Throwable {
21+
return awaitResult(1, TimeUnit.MINUTES);
22+
}
23+
24+
public T awaitResult(long time, TimeUnit timeUnit) throws Throwable {
25+
if (!countDownLatch.await(time, timeUnit)) {
26+
throw new RuntimeException("take too much time...");
27+
}
28+
if (throwable != null) {
29+
throw throwable;
30+
}
31+
return result;
32+
}
33+
}

src/main/java/com/github/fakemongo/FongoConnection.java

+7
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,13 @@ public <T> T command(String database, BsonDocument command, boolean slaveOk, Fie
540540
// new BsonInt64(0)).append("ns", new BsonString(dbCollection.getFullName()))
541541
// .append("firstBatch", FongoBsonArrayWrapper.bsonArrayWrapper(each)));
542542
return reencode(commandResultDecoder, "cursor", new BasicDBObject("id", 0L).append("ns", dbCollection.getFullName()).append("firstBatch", each));
543+
} else if (command.containsKey("listDatabases")) {
544+
final List<String> databaseNames = fongo.getDatabaseNames();
545+
final List<BsonDocument> documents = new ArrayList<BsonDocument>();
546+
for (String databaseName : databaseNames) {
547+
documents.add(new BsonDocument("name", new BsonString(databaseName)));
548+
}
549+
return (T) new BsonDocument("databases", FongoBsonArrayWrapper.bsonArrayWrapper(documents));
543550
} else {
544551
LOG.warn("Command not implemented: {}", command);
545552
throw new FongoException("Not implemented for command : " + JSON.serialize(dbObject(command)));

0 commit comments

Comments
 (0)