This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
connection_test.dart
124 lines (97 loc) · 4.23 KB
/
connection_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2018, Juan Mellado. All rights reserved. Use of this source
// is governed by a MIT-style license that can be found in the LICENSE file.
import 'dart:async' show Completer;
import 'package:test/test.dart';
import 'package:dartis/dartis.dart';
import '../redisproxy.dart';
void main() {
group('Connection', () {
test('test connectivity with a PING command', () async {
final connection = await Connection.connect('redis://localhost:6379');
final completer = Completer<void>();
// Handler.
void onData(List<int> data) async {
// PONG
expect(data, equals([RespToken.string, 80, 79, 78, 71, 13, 10]));
// Disconnect and complete the test.
await connection.disconnect();
completer.complete();
}
// Set the handler and send some raw data.
connection
..listen(onData, null, null)
..send([
RespToken.array, 49, 13, 10, // *1
RespToken.bulk, 52, 13, 10, 80, 73, 78, 71, 13, 10 // $4 PING
]);
await completer.future.timeout(Duration(seconds: 5),
onTimeout: () => throw StateError('Timeout'));
});
test('test connection done', () async {
final connection = await Connection.connect('redis://localhost:6379');
final completer = Completer<void>();
connection.done.then(completer.complete); // ignore: unawaited_futures
connection.disconnect(); // ignore: unawaited_futures
await completer.future.timeout(Duration(seconds: 5),
onTimeout: () => throw StateError('Timeout'));
});
test('attempt to send data after connection was closed', () async {
final connection = await Connection.connect('redis://localhost:6379');
final completer = Completer<void>();
connection.done.then(completer.complete); // ignore: unawaited_futures
connection.disconnect(); // ignore: unawaited_futures
await completer.future.timeout(Duration(seconds: 5),
onTimeout: () => throw StateError('Timeout'));
expect(
() => connection.send([
RespToken.array, 49, 13, 10, // *1
RespToken.bulk, 52, 13, 10, 80, 73, 78, 71, 13, 10 // $4 PING
]),
throwsA(const TypeMatcher<RedisConnectionClosedException>()));
});
test('test broken connections', () async {
final proxy = await RedisProxy.create();
final connection = await Connection.connect(proxy.connectionString);
final onData = Completer<List<int>>();
final onError = Completer<Object>();
// Sends a PING.
connection
..listen(onData.complete, (e, [st]) => onError.complete(e), null)
..send([
RespToken.array, 49, 13, 10, // *1
RespToken.bulk, 52, 13, 10, 80, 73, 78, 71, 13, 10 // $4 PING
]);
// Waits for data.
final data = await onData.future;
expect(data, equals([RespToken.string, 80, 79, 78, 71, 13, 10])); // PONG
// Closes the connection on the remote side.
await proxy.closeConnectionsAndServer();
// We now expect an exception from the connection.
expect(connection.done, throwsA(isException));
// Try to send something to get an error.
connection.send([
RespToken.array, 49, 13, 10, // *1
RespToken.bulk, 52, 13, 10, 80, 73, 78, 71, 13, 10 // $4 PING
]);
// Ensure that onError happens.
await onError.future;
});
});
group('RedisUri', () {
group('parse', () {
test('some connection strings', () {
expect(() => RedisUri.parse(''), throwsFormatException);
expect(() => RedisUri.parse('test'), throwsFormatException);
expect(() => RedisUri.parse('redis://'), throwsFormatException);
expect(() => RedisUri.parse('redis://host'), throwsFormatException);
expect(() => RedisUri.parse('redis://host:'), throwsFormatException);
expect(() => RedisUri.parse('redis://host:abc'), throwsFormatException);
expect(() => RedisUri.parse('redis://:123'), throwsFormatException);
expect(() => RedisUri.parse('host:123'), throwsFormatException);
final uri = RedisUri.parse('redis://localhost:6379');
expect(uri.host, equals('localhost'));
expect(uri.port, equals(6379));
});
});
});
}