Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DsClient | Retrieved initial cache #54

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/analyze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
# - uses: subosito/flutter-action@v2
with:
channel: 'stable'
version: 3.16.0
version: 3.19.1
# - run: |
# sudo apt-get update -y
# sudo apt-get install -y ninja-build libgtk-3-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
# - uses: subosito/flutter-action@v2
with:
channel: 'stable'
version: 3.16.0
version: 3.19.1
# - run: |
# sudo apt-get update -y
# sudo apt-get install -y ninja-build libgtk-3-dev
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: flutter-actions/setup-flutter@v2
with:
channel: 'stable'
version: 3.16.0
version: 3.19.1

- name: Clearing project
run: flutter clean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:async';

Check notice on line 1 in lib/src/core/ds_client/cache/delayed/ds_client_delayed_cache.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

100.00%
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_networking/src/core/ds_client/cache/ds_client_cache.dart';
import 'package:hmi_networking/src/core/ds_client/cache/file/ds_client_file_cache.dart';
Expand Down Expand Up @@ -33,7 +33,7 @@
_cachingTimeout = cachingTimeout;
//
@override
Future<DsDataPoint?> get(String pointName) => _primaryCache.get(pointName);
Future<Option<DsDataPoint>> get(String pointName) => _primaryCache.get(pointName);
//
@override
Future<List<DsDataPoint>> getAll() => _primaryCache.getAll();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/ds_client/cache/ds_client_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:hmi_core/hmi_core.dart';
abstract interface class DsClientCache {
///
/// Retrieves point from cache by its name.
Future<DsDataPoint?> get(String pointName);
Future<Option<DsDataPoint>> get(String pointName);
///
/// Retrieves all points from cache.
Future<List<DsDataPoint>> getAll();
Expand Down
11 changes: 7 additions & 4 deletions lib/src/core/ds_client/cache/file/ds_client_file_cache.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:hmi_core/hmi_core.dart';

Check notice on line 1 in lib/src/core/ds_client/cache/file/ds_client_file_cache.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

94.73%
import 'package:hmi_networking/src/core/ds_client/cache/ds_client_cache.dart';
import 'package:hmi_networking/src/core/ds_client/cache/file/ds_cache_file.dart';

Expand All @@ -13,10 +13,13 @@
}) : _cacheFile = cacheFile;
//
@override
Future<DsDataPoint?> get(String pointName) {
return _cacheFile.read().then(
(points) => points[pointName],
);
Future<Option<DsDataPoint>> get(String pointName) async {
final points = await _cacheFile.read();
final point = points[pointName];
return switch(point) {
null => const None() as Option<DsDataPoint>,
_ => Some(point),
};
}
//
@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:async';

Check notice on line 1 in lib/src/core/ds_client/cache/memory/ds_client_memory_cache.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

92.85%
import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_networking/src/core/ds_client/cache/ds_client_cache.dart';

Expand All @@ -15,7 +15,13 @@
}
//
@override
Future<DsDataPoint?> get(String pointName) => Future.value(_cache[pointName]);
Future<Option<DsDataPoint>> get(String pointName) async {
final point = _cache[pointName];
return switch(point) {
null => const None() as Option<DsDataPoint>,
_ => Some(point),
};
}
//
@override
Future<List<DsDataPoint>> getAll() => Future.value(_cache.values.toList());
Expand Down
17 changes: 15 additions & 2 deletions lib/src/core/ds_client/ds_client_real.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:async';

Check notice on line 1 in lib/src/core/ds_client/ds_client_real.dart

View workflow job for this annotation

GitHub Actions / coverage

Good coverage level

40.67%

import 'package:hmi_core/hmi_core.dart';
import 'package:hmi_core/hmi_core_result_new.dart';
Expand Down Expand Up @@ -265,8 +265,21 @@
/// что бы сервер прочитал и прислал значения всех точек в потоке.
/// Данные не ждем, они прийдут в потоке
@override
Future<ResultF<void>> requestAll() {
return _line.requestAll();
Future<ResultF<void>> requestAll() async {
final cache = _cache;
if (cache == null) {
return _line.requestAll();
} else {
for(final entry in _receivers.entries) {
final pointName = entry.key;
final option = await cache.get(pointName);
if(option case Some(value:final cachedPoint)) {
final controller = entry.value;
controller.add(cachedPoint);
}
}
return const Ok(null);
}
}
///
/// Делает запрос на S7 DataServer в виде списка имен точек данных
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
secondaryCache: FakeDsClientCache(),
);
for(final pointName in testPointNames) {
expect(await cache.get(pointName), isNull);
expect(await cache.get(pointName), isA<None>());
}
});
final initialCaches = [
Expand Down Expand Up @@ -54,7 +54,10 @@ void main() {
for(var i=0; i<cacheEntries.length; i++) {
final pointName = cacheEntries[i].key;
final point = cacheEntries[i].value;
expect(await cache.get(pointName), point);
final option = await cache.get(pointName);
expect(option, isA<Some>());
final receivedPoint = (option as Some<DsDataPoint>).value;
expect(receivedPoint, equals(point));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:hmi_core/hmi_core_option.dart';
import 'package:hmi_core/src/core/entities/ds_data_point.dart';
import 'package:hmi_networking/src/core/ds_client/cache/ds_client_cache.dart';

Expand All @@ -21,7 +22,13 @@ final class FakeDsClientCache implements DsClientCache {
}

@override
Future<DsDataPoint?> get(String pointName) async => internalMap[pointName];
Future<Option<DsDataPoint>> get(String pointName) async {
final point = internalMap[pointName];
return switch(point) {
null => const None() as Option<DsDataPoint>,
_ => Some(point),
};
}

@override
Future<List<DsDataPoint>> getAll() async => internalMap.values.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
cacheFile: FakeDsCacheFile(),
);
for(final pointName in testPointNames) {
expect(await cache.get(pointName), isNull);
expect(await cache.get(pointName), isA<None>());
}
});
final initialCaches = [
Expand Down Expand Up @@ -54,7 +54,10 @@ void main() {
for(var i=0; i<cacheEntries.length; i++) {
final pointName = cacheEntries[i].key;
final point = cacheEntries[i].value;
expect(await cache.get(pointName), point);
final option = await cache.get(pointName);
expect(option, isA<Some>());
final receivedPoint = (option as Some<DsDataPoint>).value;
expect(receivedPoint, equals(point));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ void main() {
final cache = DsClientMemoryCache();
for(final point in testPoints) {
await cache.add(point);
expect(await cache.get(point.name.name), equals(point));
final option = await cache.get(point.name.name);
expect(option, isA<Some>());
final receivedPoint = (option as Some<DsDataPoint>).value;
expect(receivedPoint, equals(point));
}
});
test('addMany(points) inserts multiple points to its internal state', () async {
Expand Down Expand Up @@ -57,8 +60,11 @@ void main() {
DsDataPoint oldPoint = uniqueTestPoints[0];
await cache.add(oldPoint);
for(final point in uniqueTestPoints.sublist(1)) {
final optionPrevious = await cache.get(pointName);
expect(optionPrevious, isA<Some>());
final receivedPointPrevious = (optionPrevious as Some<DsDataPoint>).value;
expect(
await cache.get(pointName), equals(oldPoint),
receivedPointPrevious, equals(oldPoint),
reason: 'Should be equal to old value before an addition.',
);
final newPoint = point;
Expand All @@ -67,8 +73,11 @@ void main() {
reason: 'Points should have different attributes in this test.',
);
await cache.add(newPoint);
final optionCurrent = await cache.get(pointName);
expect(optionCurrent, isA<Some>());
final receivedPointCurrent = (optionCurrent as Some<DsDataPoint>).value;
expect(
await cache.get(pointName), equals(newPoint),
receivedPointCurrent, equals(newPoint),
reason: 'Should be equal to new value after an addition.',
);
oldPoint = newPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {
final testPointNames = ['Winch2.EncoderBR1', 'ConstantTension.Active', 'HPA.LowNiroPressure', 'HPU.HighPressure', 'Winch1.Overload'];
final cache = DsClientMemoryCache();
for(final pointName in testPointNames) {
expect(await cache.get(pointName), isNull);
expect(await cache.get(pointName), isA<None>());
}
});
final initialCaches = [
Expand All @@ -32,7 +32,7 @@ void main() {
test('getAll() returns provided initial cache', () async {
for(final initialCache in initialCaches) {
final cache = DsClientMemoryCache(initialCache: initialCache);
expect(await cache.getAll(), initialCache.values.toList());
expect(await cache.getAll(), equals(initialCache.values.toList()));
}
});
test('get(pointName) returns provided initial cache', () async {
Expand All @@ -42,7 +42,10 @@ void main() {
for(var i=0; i<cacheEntries.length; i++) {
final pointName = cacheEntries[i].key;
final point = cacheEntries[i].value;
expect(await cache.get(pointName), point);
final option = await cache.get(pointName);
expect(option, isA<Some>());
final receivedPoint = (option as Some<DsDataPoint>).value;
expect(receivedPoint, equals(point));
}
}
});
Expand Down
Loading