Skip to content

Commit

Permalink
run dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
erf authored and ra1u committed Jan 10, 2024
1 parent 23af8c6 commit 9eafaef
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 69 deletions.
7 changes: 3 additions & 4 deletions lib/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ class Command {
this.serializer = other.serializer;
}

Command setParser(Parser p){
Command setParser(Parser p) {
this.parser = p;
return this;
}

Command setSerializer(Serializer s){
Command setSerializer(Serializer s) {
this.serializer = s;
return this;
}


/// Serialize and send data to server
///
Expand All @@ -44,7 +43,7 @@ class Command {
/// send_object(["SET","key","value"]);
Future send_object(Object obj) {
try {
return _connection._sendraw(parser,serializer.serialize(obj)).then((v) {
return _connection._sendraw(parser, serializer.serialize(obj)).then((v) {
// turn RedisError into exception
if (v is RedisError) {
return Future.error(v);
Expand Down
2 changes: 1 addition & 1 deletion lib/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RedisConnection {
}

// ignore: unused_element
Future _sendraw(Parser parser,List<int> data) {
Future _sendraw(Parser parser, List<int> data) {
_socket!.add(data);
return _senddummy(parser);
}
Expand Down
24 changes: 16 additions & 8 deletions lib/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@

part of redis;


// this class is returned when redis response is type error
class RedisError {
class RedisError {
String e;
RedisError(this.e);
String toString() { return "RedisError($e)";}
String toString() {
return "RedisError($e)";
}

String get error => e;
}

// thiss class is returned when parsing in client side (aka this libraray)
// get error
class RedisRuntimeError {
class RedisRuntimeError {
String e;
RedisRuntimeError(this.e);
String toString() { return "RedisRuntimeError($e)";}
String toString() {
return "RedisRuntimeError($e)";
}

String get error => e;
}

class TransactionError {
class TransactionError {
String e;
TransactionError(this.e);
String toString() { return "TranscationError($e)";}
String get error => e;
String toString() {
return "TranscationError($e)";
}

String get error => e;
}
2 changes: 1 addition & 1 deletion lib/lazystream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class StreamNext {
// close socket on error
// follow bug https://github.com/ra1u/redis-dart/issues/49
// and bug https://github.com/dart-lang/sdk/issues/47538
socket.close().then<void>((_){
socket.close().then<void>((_) {
if (_nfut >= 1) {
_nfut = 0;
for (Completer<List<int>> e in _queue) {
Expand Down
2 changes: 0 additions & 2 deletions lib/redis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* Luka Rahne
*/


library redis;

import 'dart:io';
Expand All @@ -24,4 +23,3 @@ part './command.dart';
part './pubsub.dart';
part './cas.dart';
part './exceptions.dart';

16 changes: 7 additions & 9 deletions lib/redisparser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

part of redis;

class RedisParser extends Parser {

}
class RedisParser extends Parser {}

class RedisParserBulkBinary extends Parser {
Future parseBulk(LazyStream s) {
Expand All @@ -21,17 +19,17 @@ class RedisParserBulkBinary extends Parser {
return null;
if (i >= 0) {
//i of bulk data
return s.take_n(i).then((lst) => takeCRLF(
s, lst)); //consume CRLF and return list
return s
.take_n(i)
.then((lst) => takeCRLF(s, lst)); //consume CRLF and return list
} else {
return Future.error(
RedisRuntimeError("cant process buld data less than -1"));
}
});
}
}
}


class Parser {
static final UTF8 = const Utf8Codec();
static const int CR = 13;
Expand Down Expand Up @@ -72,10 +70,10 @@ class Parser {
});
}

Future parse(LazyStream s){
Future parse(LazyStream s) {
return parseredisresponse(s);
}

Future parseredisresponse(LazyStream s) {
return s.take_n(1).then((list) {
int cmd = list[0];
Expand Down
5 changes: 2 additions & 3 deletions lib/redisserialise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ class RedisBulk {

typedef Consumer = void Function(Iterable<int> s);


class Serializer {
List<int> serialize(Object? object){
List<int> serialize(Object? object) {
return RedisSerialize.Serialize(object);
}
}
}

class RedisSerialize {
Expand Down
4 changes: 3 additions & 1 deletion lib/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class Transaction extends Command {
c.completeError(
RedisError("Could not enqueue command: " + msg.toString()));
}
}).catchError((error){ c.completeError(error); });
}).catchError((error) {
c.completeError(error);
});
return c.future;
}

Expand Down
15 changes: 7 additions & 8 deletions test/binary_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import 'package:test/test.dart';
import 'main.dart';
import 'dart:math';


List<int> _gen_rnd_array(Random rng){
List<int> _gen_rnd_array(Random rng) {
int len = 1 + rng.nextInt(1000);
List<int> r = List<int>.filled(len,0);
for(int i=0;i<len;++i){
List<int> r = List<int>.filled(len, 0);
for (int i = 0; i < len; ++i) {
r[i] = rng.nextInt(255);
}
return r;
Expand All @@ -22,18 +21,18 @@ void main() {
Command _cmd = await generate_connect();
Command cmd_bin = Command.from(_cmd).setParser(RedisParserBulkBinary());

List<int> d = [1,2,3,4,5,6,7,8,9];
List<int> d = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var r = await cmd_bin.send_object(["SET", key, RedisBulk(d)]);
expect(r, equals("OK"));
expect(await cmd_bin.send_object(["GET", key]), equals(d));
});

test("binary with randomly generated data", () async {
Command _cmd = await generate_connect();
Command cmd_bin = Command.from(_cmd).setParser(RedisParserBulkBinary());
var rng = Random();
var rng = Random();

for(int i = 0;i < 1000; ++i){
for (int i = 0; i < 1000; ++i) {
List<int> d = _gen_rnd_array(rng);
var r = await cmd_bin.send_object(["SET", key, RedisBulk(d)]);
expect(r, equals("OK"));
Expand Down
2 changes: 1 addition & 1 deletion test/close_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main() {
});

test("Open/Close in loop", () async {
for( int i=0;i<1000;++i) {
for (int i = 0; i < 1000; ++i) {
Command cmd = await generate_connect();
await cmd.send_object(["SET", "test", "0"]);
await cmd.get_connection().close();
Expand Down
3 changes: 1 addition & 2 deletions test/conversion_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ void main() {
expect(cmd.send_object(["SADD", "test_list", 1]), completion(isA<int>()));
});
});

}
}
21 changes: 12 additions & 9 deletions test/lua_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ void main() {
test("Test Lua Native", () async {
Command cmd = await generate_connect();

expect((await cmd.send_object([
"EVAL",
"return {KEYS[1],{KEYS[2],{ARGV[1]},ARGV[2]},2}",
"2",
"key1",
"key2",
"first",
"2"
])).toString(), equals("[key1, [key2, [first], 2], 2]"));
expect(
(await cmd.send_object([
"EVAL",
"return {KEYS[1],{KEYS[2],{ARGV[1]},ARGV[2]},2}",
"2",
"key1",
"key2",
"first",
"2"
]))
.toString(),
equals("[key1, [key2, [first], 2], 2]"));
});
}
5 changes: 2 additions & 3 deletions test/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ int g_db_port = 0;

void init_db_vars() {
// read REDIS_URL and REDIS_PORT from ENV and store in globals for faster retreival
if(g_redis_initialsed)
return;
if (g_redis_initialsed) return;
Map<String, String> envVars = Platform.environment;
g_db_uri = envVars["REDIS_URL"] ?? "localhost";
String port = envVars["REDIS_PORT"] ?? "6379";
g_db_port = int.tryParse(port) ?? 6379;
g_redis_initialsed = true;
}
}

Future<Command> generate_connect() {
init_db_vars();
Expand Down
28 changes: 13 additions & 15 deletions test/pubsub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ void main() async {
Command cmdP = await generate_connect();
Command cmdS = await generate_connect();

group("Test Redis Pub-Sub", () {

group("Test Redis Pub-Sub", () {
PubSub subscriber = PubSub(cmdS);

test("Publishing to channel before subscription", () {
Expand Down Expand Up @@ -58,18 +57,17 @@ void main() async {
"Publishing a message after unsubscribe should be received by zero clients.");

// TODO: Multiple channels, Pattern (un)subscribe
});

test("Test close", () async {
// test that we can close connection
// creates new connection as prevously used in test
// does not expect errors
Command cmdClose = await generate_connect();
PubSub ps_c = PubSub(cmdClose);
cmdClose.get_connection().close();
expect(ps_c.getStream(),
emitsError(anything), // todo catch CloseError
reason: "Number of subscribers should be 0 after unsubscribe");
});
});

test("Test close", () async {
// test that we can close connection
// creates new connection as prevously used in test
// does not expect errors
Command cmdClose = await generate_connect();
PubSub ps_c = PubSub(cmdClose);
cmdClose.get_connection().close();
expect(ps_c.getStream(), emitsError(anything), // todo catch CloseError
reason: "Number of subscribers should be 0 after unsubscribe");
});
});
}
3 changes: 1 addition & 2 deletions test/unicode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ void main() {
Command cmd = await generate_connect();
String unicodeString = "中华人民共和😊👍📱😀😬";


expect(cmd.send_object(["SET", "unicode_test", unicodeString]),
completion(equals("OK")));

expect(cmd.send_object(["GET", "unicode_test"]),
completion(equals(unicodeString)));
});
});
}
}

0 comments on commit 9eafaef

Please sign in to comment.