Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor and remove RedisConnection

  • Loading branch information...
commit a0e316bbbe84a900b2038c08210f0b4a4bd6c644 1 parent fe765bb
@MarkBennett authored
View
5 src/redis-coder.dart
@@ -13,4 +13,9 @@ class RedisCoder {
});
return encodeUtf8(buffer.toString());
}
+ static List decode(List bytes) {
+ List message = new List();
+
+ return ["SET", "mykey", "myvalue"];
+ }
}
View
40 src/redis-connection.dart
@@ -1,40 +0,0 @@
-class RedisConnection {
- String hostname;
- bool _connected;
- Socket _conn;
-
- bool get connected() => _connected;
-
- RedisConnection(this.hostname) {
- _connected = true;
- _conn = new Socket(hostname, 6379);
- }
-
- RedisConnection set(String key, String value) {
- // dummy
- return this;
- }
-
- Future cmd(List<String> args) {
- Completer completer = new Completer<String>();
- Future future = completer.future;
- completer.complete(["value1"]);
-
- return future;
- }
-
- RedisConnection get(String key, void onComplete(String)) {
- cmd(["GET", key]).then((results) => onComplete(results[0]));
-
- return this;
- }
-
- void _getCallback() {
-
- }
-
- close() {
- _conn.close();
- _connected = false;
- }
-}
View
20 src/redis.dart
@@ -3,13 +3,21 @@
#import("dart:io");
#import("dart:utf");
-#source("redis-connection.dart");
#source("redis-coder.dart");
-class Redis {
- static Future<RedisConnection> connect(hostname) {
- Completer completer = new Completer();
- completer.complete(new RedisConnection(hostname));
- return completer.future;
+class RedisClient {
+ Completer<RedisClient> _connectCompleter;
+ Future<RedisClient> get connect() => _connectCompleter.future;
+
+ Socket _socket;
+
+ RedisClient(host, [Socket socket]) {
+ if (null == socket) {
+ socket = new Socket(host, 3000);
+ }
+ _socket = socket;
+
+ _connectCompleter = new Completer<RedisClient>();
+ _socket.onConnect = () => _connectCompleter.complete(this);
}
}
View
54 tests/test_suites.dart
@@ -6,44 +6,24 @@
#import('../src/redis.dart');
#import('dart:utf');
+// A typical session using the Redis client goes like this
+//
+// new RedisClient("localhost").connect.then((client) => client.ping());
testSuites() {
- describe("Redis.connect", () {
- Future<RedisConnection> future;
- RedisConnection conn;
+ describe("RedisClient", () {
+ Future<RedisClient> future;
+ RedisClient client;
+ var callback_client;
beforeEach(() {
- future = Redis.connect("localhost");
- future.then((connection) => conn = connection );
+ client = new RedisClient("localhost", new MockSocket());
+ future = client.connect;
+ future.then((client) => callback_client = client );
});
it("should return a future", () => expect(future).to(not(beNull())));
- it("should set the hostname on the connection", () => expect(conn.hostname).to(equal("localhost")));
- });
-
- describe("RedisConnection", () {
- RedisConnection conn;
-
- beforeEach(() => conn = new RedisConnection("localhost"));
-
- it('should be connected', () => expect(conn.connected).to(beTrue()));
-
- describe("after disconnecting", () {
- beforeEach(() => conn.close());
-
- it('should not be connected', () => expect(conn.connected).to(beFalse()));
- });
-
- describe("setting a key", () {
- beforeEach(() => conn.set("test", "value1"));
-
- it("should set the key in redis", () {
- var ret_val;
-
- conn.get("test", (val) => ret_val = val);
- expect(ret_val).to(equal("value1"));
- });
- });
+ it("should invoke the future with the client after connecting", () => expect(callback_client).to(equal(client)));
});
describe("RedisCoder", () {
@@ -51,8 +31,18 @@ testSuites() {
it("should encode a single argument message", () {
expect(RedisCoder.encode(["PING"])).to(beEquivalent(encodeUtf8("*1\r\n\$4\r\nPING\r\n")));
});
- it("should encode a multi argment methd", () {
+ it("should encode a multi argument message", () {
expect(RedisCoder.encode(["SET", "mykey", "myvalue"])).to(beEquivalent(encodeUtf8("*3\r\n\$3\r\nSET\r\n\$5\r\nmykey\r\n\$7\r\nmyvalue\r\n")));
});
+
+ it("should decode a multi argument message", () {
+ expect(RedisCoder.decode(encodeUtf8("*3\r\n\$3\r\nSET\r\n\$5\r\nmykey\r\n\$7\r\nmyvalue\r\n"))).to(beEquivalent(["SET", "mykey", "myvalue"]));
+ });
});
+}
+
+class MockSocket {
+ void set onConnect(void callback()) {
+ callback();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.