Skip to content

Commit 75f8038

Browse files
authored
hamed/increase_call_manager_code_coverage (#255)
increase call manager code coverage
1 parent ddb58e3 commit 75f8038

File tree

6 files changed

+474
-15
lines changed

6 files changed

+474
-15
lines changed

lib/services/connection/call_manager/call_history.dart

+8-15
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class CallHistory {
1616
set limit(int limit) {
1717
_limit = limit;
1818

19-
trimIncoming();
20-
trimOutgoing();
19+
_trimHistory(incoming);
20+
_trimHistory(outgoing);
2121
}
2222

2323
/// Record a message that was received from the remote endpoint
@@ -30,7 +30,7 @@ class CallHistory {
3030
CallHistoryEntry(timeStamp: timestamp, method: method, message: message),
3131
);
3232

33-
trimIncoming();
33+
_trimHistory(incoming);
3434
}
3535

3636
/// Record a message being sent to the remote endpoint
@@ -43,20 +43,13 @@ class CallHistory {
4343
CallHistoryEntry(timeStamp: timestamp, method: method, message: message),
4444
);
4545

46-
trimOutgoing();
46+
_trimHistory(outgoing);
4747
}
4848

49-
/// Trim early entries from [incoming] if we exceed the current limit
50-
void trimIncoming() {
51-
if (incoming.length >= limit) {
52-
incoming.removeRange(0, incoming.length - limit + 1);
53-
}
54-
}
55-
56-
/// Trim early entries from [outgoing] if we exceed the current limit
57-
void trimOutgoing() {
58-
if (outgoing.length >= limit) {
59-
outgoing.removeRange(0, outgoing.length - limit + 1);
49+
/// Trim early entries from [CallHistory] if we exceed the current limit
50+
void _trimHistory(List<CallHistoryEntry> callHistory) {
51+
if (callHistory.length > limit) {
52+
callHistory.removeRange(0, callHistory.length - limit);
6053
}
6154
}
6255
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'dart:async';
2+
3+
import 'package:test/test.dart';
4+
5+
import 'package:flutter_deriv_api/api/models/enums.dart';
6+
import 'package:flutter_deriv_api/basic_api/generated/forget_all_receive.dart';
7+
import 'package:flutter_deriv_api/basic_api/generated/forget_receive.dart';
8+
import 'package:flutter_deriv_api/basic_api/request.dart';
9+
import 'package:flutter_deriv_api/basic_api/response.dart';
10+
import 'package:flutter_deriv_api/services/connection/api_manager/base_api.dart';
11+
import 'package:flutter_deriv_api/services/connection/api_manager/connection_information.dart';
12+
import 'package:flutter_deriv_api/services/connection/call_manager/base_call_manager.dart';
13+
14+
void main() {
15+
group('BaseCallManager tests =>', () {
16+
late BaseCallManager<MockResponse> callManager;
17+
late MockAPI mockAPI;
18+
19+
setUp(() {
20+
mockAPI = MockAPI();
21+
callManager = MockCallManager(mockAPI);
22+
});
23+
24+
test('should add a request to the channel and return a response.',
25+
() async {
26+
final MockRequest request = MockRequest();
27+
final MockResponse response = MockResponse();
28+
29+
final Completer<Response> completer = Completer<Response>();
30+
31+
unawaited(
32+
callManager
33+
.addToChannel(request: request)
34+
.then((Response value) => completer.complete(value)),
35+
);
36+
37+
callManager.handleResponse(
38+
requestId: 1,
39+
response: <String, dynamic>{'msg_type': 'response'},
40+
);
41+
42+
completer.complete(response);
43+
44+
expect(await completer.future, response);
45+
});
46+
});
47+
}
48+
49+
class MockCallManager extends BaseCallManager<MockResponse> {
50+
MockCallManager(BaseAPI api) : super(api);
51+
52+
@override
53+
MockResponse call({required Request request}) {
54+
throw UnimplementedError();
55+
}
56+
}
57+
58+
class MockAPI implements BaseAPI {
59+
@override
60+
void addToChannel(Map<String, dynamic> request) {}
61+
62+
@override
63+
Future<T> call<T>({required Request request}) => throw UnimplementedError();
64+
65+
@override
66+
Future<void> connect(
67+
ConnectionInformation? connectionInformation, {
68+
ConnectionCallback? onDone,
69+
ConnectionCallback? onOpen,
70+
ConnectionCallback? onError,
71+
bool printResponse = false,
72+
}) =>
73+
throw UnimplementedError();
74+
75+
@override
76+
Future<void> disconnect() => throw UnimplementedError();
77+
78+
@override
79+
bool get enableDebug => throw UnimplementedError();
80+
81+
@override
82+
String get key => throw UnimplementedError();
83+
84+
@override
85+
Stream<Response>? subscribe({
86+
required Request request,
87+
RequestCompareFunction? comparePredicate,
88+
}) =>
89+
throw UnimplementedError();
90+
91+
@override
92+
Future<ForgetReceive> unsubscribe({required String subscriptionId}) =>
93+
throw UnimplementedError();
94+
95+
@override
96+
Future<ForgetAllReceive> unsubscribeAll({required ForgetStreamType method}) =>
97+
throw UnimplementedError();
98+
}
99+
100+
class MockRequest extends Request {}
101+
102+
class MockResponse extends Response {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:test/test.dart';
2+
3+
import 'package:flutter_deriv_api/services/connection/call_manager/call_history_entry.dart';
4+
5+
void main() {
6+
group('CallHistoryEntry tests =>', () {
7+
test('initialization with values.', () {
8+
const int timeStamp = 1621188000;
9+
const String method = 'test';
10+
11+
final Map<String, dynamic> message = <String, dynamic>{
12+
'content': 'Hello'
13+
};
14+
15+
final CallHistoryEntry entry = CallHistoryEntry(
16+
timeStamp: timeStamp,
17+
method: method,
18+
message: message,
19+
);
20+
21+
expect(entry.timeStamp, equals(timeStamp));
22+
expect(entry.method, equals(method));
23+
expect(entry.message, equals(message));
24+
});
25+
26+
test('initialization with null values.', () {
27+
final CallHistoryEntry entry = CallHistoryEntry();
28+
29+
expect(entry.timeStamp, isNull);
30+
expect(entry.method, isNull);
31+
expect(entry.message, isNull);
32+
});
33+
});
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import 'package:test/test.dart';
2+
import 'package:flutter_deriv_api/services/connection/call_manager/call_history.dart';
3+
4+
void main() {
5+
group('CallHistory tests =>', () {
6+
late CallHistory callHistory;
7+
8+
setUp(() {
9+
callHistory = CallHistory();
10+
});
11+
12+
test('test pushIncoming method.', () {
13+
const int timestamp = 1621188000;
14+
const String method = 'test';
15+
16+
final Map<String, dynamic> message = <String, dynamic>{
17+
'content': 'Hello'
18+
};
19+
20+
callHistory.pushIncoming(
21+
timestamp: timestamp,
22+
method: method,
23+
message: message,
24+
);
25+
26+
expect(callHistory.incoming.length, equals(1));
27+
expect(callHistory.incoming.first.timeStamp, equals(timestamp));
28+
expect(callHistory.incoming.first.method, equals(method));
29+
expect(callHistory.incoming.first.message, equals(message));
30+
});
31+
32+
test('test pushOutgoing method.', () {
33+
const int timestamp = 1621188000;
34+
const String method = 'test';
35+
36+
final Map<String, dynamic> message = <String, dynamic>{
37+
'content': 'World'
38+
};
39+
40+
callHistory.pushOutgoing(
41+
timestamp: timestamp,
42+
method: method,
43+
message: message,
44+
);
45+
46+
expect(callHistory.outgoing.length, equals(1));
47+
expect(callHistory.outgoing.first.timeStamp, equals(timestamp));
48+
expect(callHistory.outgoing.first.method, equals(method));
49+
expect(callHistory.outgoing.first.message, equals(message));
50+
});
51+
52+
test('should trim history based on limit for incomming responses.', () {
53+
const int limit = 2;
54+
55+
callHistory.limit = limit;
56+
57+
const int timestamp1 = 1621188000;
58+
const String method1 = 'test 1';
59+
60+
final Map<String, dynamic> message1 = <String, dynamic>{
61+
'content': 'Hello'
62+
};
63+
64+
callHistory.pushIncoming(
65+
timestamp: timestamp1,
66+
method: method1,
67+
message: message1,
68+
);
69+
70+
const int timestamp2 = 1621189000;
71+
const String method2 = 'test 2';
72+
73+
final Map<String, dynamic> message2 = <String, dynamic>{
74+
'content': 'World'
75+
};
76+
77+
callHistory.pushIncoming(
78+
timestamp: timestamp2,
79+
method: method2,
80+
message: message2,
81+
);
82+
83+
const int timestamp3 = 1621190000;
84+
const String method3 = 'test 3';
85+
86+
final Map<String, dynamic> message3 = <String, dynamic>{
87+
'content': 'Test'
88+
};
89+
90+
callHistory.pushIncoming(
91+
timestamp: timestamp3,
92+
method: method3,
93+
message: message3,
94+
);
95+
96+
expect(callHistory.incoming.length, equals(limit));
97+
expect(callHistory.incoming.first.timeStamp, equals(timestamp2));
98+
expect(callHistory.incoming.first.method, equals(method2));
99+
expect(callHistory.incoming.first.message, equals(message2));
100+
expect(callHistory.incoming.last.timeStamp, equals(timestamp3));
101+
expect(callHistory.incoming.last.method, equals(method3));
102+
expect(callHistory.incoming.last.message, equals(message3));
103+
});
104+
105+
test('should trim history based on limit for outgoing responses.', () {
106+
const int limit = 2;
107+
108+
callHistory.limit = limit;
109+
110+
const int timestamp1 = 1621188000;
111+
const String method1 = 'test 1';
112+
113+
final Map<String, dynamic> message1 = <String, dynamic>{
114+
'content': 'Hello'
115+
};
116+
117+
callHistory.pushOutgoing(
118+
timestamp: timestamp1,
119+
method: method1,
120+
message: message1,
121+
);
122+
123+
const int timestamp2 = 1621189000;
124+
const String method2 = 'test 2';
125+
126+
final Map<String, dynamic> message2 = <String, dynamic>{
127+
'content': 'World'
128+
};
129+
130+
callHistory.pushOutgoing(
131+
timestamp: timestamp2,
132+
method: method2,
133+
message: message2,
134+
);
135+
136+
const int timestamp3 = 1621190000;
137+
const String method3 = 'test 3';
138+
139+
final Map<String, dynamic> message3 = <String, dynamic>{
140+
'content': 'Test'
141+
};
142+
143+
callHistory.pushOutgoing(
144+
timestamp: timestamp3,
145+
method: method3,
146+
message: message3,
147+
);
148+
149+
expect(callHistory.outgoing.length, equals(limit));
150+
expect(callHistory.outgoing.first.timeStamp, equals(timestamp2));
151+
expect(callHistory.outgoing.first.method, equals(method2));
152+
expect(callHistory.outgoing.first.message, equals(message2));
153+
expect(callHistory.outgoing.last.timeStamp, equals(timestamp3));
154+
expect(callHistory.outgoing.last.method, equals(method3));
155+
expect(callHistory.outgoing.last.message, equals(message3));
156+
});
157+
});
158+
}

0 commit comments

Comments
 (0)