refactor, use external libraries, and add integration test for veilid_support

This commit is contained in:
Christien Rioux 2024-05-01 20:58:25 -04:00
parent e622b7f949
commit 25a6a00fcf
84 changed files with 626 additions and 3835 deletions

View file

@ -1,19 +1,30 @@
@Timeout(Duration(seconds: 60))
@Timeout(Duration(seconds: 120))
library veilid_support_integration_test;
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:veilid_test/veilid_test.dart';
import 'fixtures.dart';
import 'fixtures/fixtures.dart';
import 'test_dht_record_pool.dart';
import 'test_dht_short_array.dart';
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
final fixture = DefaultFixture();
final veilidFixture =
DefaultVeilidFixture(programName: 'veilid_support integration test');
final updateProcessorFixture =
UpdateProcessorFixture(veilidFixture: veilidFixture);
final tickerFixture =
TickerFixture(updateProcessorFixture: updateProcessorFixture);
final dhtRecordPoolFixture = DHTRecordPoolFixture(
tickerFixture: tickerFixture,
updateProcessorFixture: updateProcessorFixture);
group('Started Tests', () {
setUpAll(fixture.setUp);
tearDownAll(fixture.tearDown);
setUpAll(veilidFixture.setUp);
tearDownAll(veilidFixture.tearDown);
// group('Crypto Tests', () {
// test('best cryptosystem', testBestCryptoSystem);
@ -23,15 +34,32 @@ void main() {
// });
group('Attached Tests', () {
setUpAll(fixture.attach);
tearDownAll(fixture.detach);
setUpAll(veilidFixture.attach);
tearDownAll(veilidFixture.detach);
group('DHT Support Tests', () {
group('DHTRecordPool Tests', () {
test('create pool', testDHTRecordPoolCreate);
});
setUpAll(updateProcessorFixture.setUp);
setUpAll(tickerFixture.setUp);
tearDownAll(tickerFixture.tearDown);
tearDownAll(updateProcessorFixture.tearDown);
test('create pool', testDHTRecordPoolCreate);
// group('DHTRecordPool Tests', () {
// setUpAll(dhtRecordPoolFixture.setUp);
// tearDownAll(dhtRecordPoolFixture.tearDown);
// test('create/delete record', testDHTRecordCreateDelete);
// test('record scopes', testDHTRecordScopes);
// test('create/delete deep record', testDHTRecordDeepCreateDelete);
// });
group('DHTShortArray Tests', () {
test('create shortarray', testDHTShortArrayCreate);
setUpAll(dhtRecordPoolFixture.setUp);
tearDownAll(dhtRecordPoolFixture.tearDown);
// test('create shortarray', testDHTShortArrayCreateDelete);
test('add shortarray', testDHTShortArrayAdd);
});
});
});

View file

@ -1,152 +0,0 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:mutex/mutex.dart';
import 'package:veilid/veilid.dart';
class DefaultFixture {
DefaultFixture();
StreamSubscription<VeilidUpdate>? _veilidUpdateSubscription;
Stream<VeilidUpdate>? _veilidUpdateStream;
final StreamController<VeilidUpdate> _updateStreamController =
StreamController.broadcast();
static final _fixtureMutex = Mutex();
Future<void> setUp() async {
await _fixtureMutex.acquire();
assert(_veilidUpdateStream == null, 'should not set up fixture twice');
final ignoreLogTargetsStr =
// ignore: do_not_use_environment
const String.fromEnvironment('IGNORE_LOG_TARGETS').trim();
final ignoreLogTargets = ignoreLogTargetsStr.isEmpty
? <String>[]
: ignoreLogTargetsStr.split(',').map((e) => e.trim()).toList();
final Map<String, dynamic> platformConfigJson;
if (kIsWeb) {
final platformConfig = VeilidWASMConfig(
logging: VeilidWASMConfigLogging(
performance: VeilidWASMConfigLoggingPerformance(
enabled: true,
level: VeilidConfigLogLevel.debug,
logsInTimings: true,
logsInConsole: false,
ignoreLogTargets: ignoreLogTargets,
),
api: VeilidWASMConfigLoggingApi(
enabled: true,
level: VeilidConfigLogLevel.info,
ignoreLogTargets: ignoreLogTargets,
)));
platformConfigJson = platformConfig.toJson();
} else {
final platformConfig = VeilidFFIConfig(
logging: VeilidFFIConfigLogging(
terminal: VeilidFFIConfigLoggingTerminal(
enabled: false,
level: VeilidConfigLogLevel.debug,
ignoreLogTargets: ignoreLogTargets,
),
otlp: VeilidFFIConfigLoggingOtlp(
enabled: false,
level: VeilidConfigLogLevel.trace,
grpcEndpoint: 'localhost:4317',
serviceName: 'Veilid Tests',
ignoreLogTargets: ignoreLogTargets,
),
api: VeilidFFIConfigLoggingApi(
enabled: true,
// level: VeilidConfigLogLevel.debug,
level: VeilidConfigLogLevel.info,
ignoreLogTargets: ignoreLogTargets,
)));
platformConfigJson = platformConfig.toJson();
}
Veilid.instance.initializeVeilidCore(platformConfigJson);
var config = await getDefaultVeilidConfig(
isWeb: kIsWeb,
programName: 'Veilid Tests',
// ignore: avoid_redundant_argument_values, do_not_use_environment
bootstrap: const String.fromEnvironment('BOOTSTRAP'),
// ignore: avoid_redundant_argument_values, do_not_use_environment
networkKeyPassword: const String.fromEnvironment('NETWORK_KEY'),
);
config =
config.copyWith(tableStore: config.tableStore.copyWith(delete: true));
config = config.copyWith(
protectedStore: config.protectedStore.copyWith(delete: true));
config =
config.copyWith(blockStore: config.blockStore.copyWith(delete: true));
final us =
_veilidUpdateStream = await Veilid.instance.startupVeilidCore(config);
_veilidUpdateSubscription = us.listen((update) {
if (update is VeilidLog) {
// print(update.message);
} else if (update is VeilidUpdateAttachment) {
} else if (update is VeilidUpdateConfig) {
} else if (update is VeilidUpdateNetwork) {
} else if (update is VeilidAppMessage) {
} else if (update is VeilidAppCall) {
} else if (update is VeilidUpdateValueChange) {
} else if (update is VeilidUpdateRouteChange) {
} else {
throw Exception('unexpected update: $update');
}
_updateStreamController.sink.add(update);
});
}
Stream<VeilidUpdate> get updateStream => _updateStreamController.stream;
Future<void> attach() async {
await Veilid.instance.attach();
// Wait for attached state
while (true) {
final state = await Veilid.instance.getVeilidState();
var done = false;
if (state.attachment.publicInternetReady) {
switch (state.attachment.state) {
case AttachmentState.detached:
break;
case AttachmentState.attaching:
break;
case AttachmentState.detaching:
break;
default:
done = true;
break;
}
}
if (done) {
break;
}
await Future.delayed(const Duration(seconds: 1));
}
}
Future<void> detach() async {
await Veilid.instance.detach();
}
Future<void> tearDown() async {
assert(_veilidUpdateStream != null, 'should not tearDown without setUp');
final cancelFut = _veilidUpdateSubscription?.cancel();
await Veilid.instance.shutdownVeilidCore();
await cancelFut;
_veilidUpdateSubscription = null;
_veilidUpdateStream = null;
_fixtureMutex.release();
}
}

View file

@ -0,0 +1,36 @@
import 'dart:async';
import 'package:async_tools/async_tools.dart';
import 'package:veilid_support/veilid_support.dart';
import 'package:veilid_test/veilid_test.dart';
class DHTRecordPoolFixture implements TickerFixtureTickable {
DHTRecordPoolFixture(
{required this.tickerFixture, required this.updateProcessorFixture});
static final _fixtureMutex = Mutex();
UpdateProcessorFixture updateProcessorFixture;
TickerFixture tickerFixture;
Future<void> setUp() async {
await _fixtureMutex.acquire();
await DHTRecordPool.init();
tickerFixture.register(this);
}
Future<void> tearDown() async {
assert(_fixtureMutex.isLocked, 'should not tearDown without setUp');
tickerFixture.unregister(this);
await DHTRecordPool.close();
_fixtureMutex.release();
}
@override
Future<void> onTick() async {
if (!updateProcessorFixture
.processorConnectionState.isPublicInternetReady) {
return;
}
await DHTRecordPool.instance.tick();
}
}

View file

@ -0,0 +1 @@
export 'dht_record_pool_fixture.dart';

View file

@ -1,9 +1,201 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:veilid_support/veilid_support.dart';
Future<void> testDHTRecordPoolCreate() async {
// final cs = await Veilid.instance.bestCryptoSystem();
// expect(await cs.defaultSaltLength(), equals(16));
await DHTRecordPool.init(logger: debugPrintSynchronously);
final pool = DHTRecordPool.instance;
await pool.tick();
await DHTRecordPool.close();
}
Future<void> testDHTRecordCreateDelete() async {
final pool = DHTRecordPool.instance;
// Close before delete
{
final rec = await pool.createRecord(debugName: 'test_create_delete 1');
expect(rec.isOpen, isTrue);
await rec.close();
expect(rec.isOpen, isFalse);
await pool.deleteRecord(rec.key);
// Set should fail
await expectLater(() async => rec.tryWriteBytes(utf8.encode('test')),
throwsA(isA<VeilidAPIException>()));
}
// Close after delete
{
final rec2 = await pool.createRecord(debugName: 'test_create_delete 2');
expect(rec2.isOpen, isTrue);
await pool.deleteRecord(rec2.key);
expect(rec2.isOpen, isTrue);
await rec2.close();
expect(rec2.isOpen, isFalse);
// Set should fail
await expectLater(() async => rec2.tryWriteBytes(utf8.encode('test')),
throwsA(isA<VeilidAPIException>()));
}
// Close after delete multiple
// Okay to request delete multiple times before close
{
final rec3 = await pool.createRecord(debugName: 'test_create_delete 3');
await pool.deleteRecord(rec3.key);
await pool.deleteRecord(rec3.key);
// Set should succeed still
await rec3.tryWriteBytes(utf8.encode('test'));
await rec3.close();
await rec3.close();
// Set should fail
await expectLater(() async => rec3.tryWriteBytes(utf8.encode('test')),
throwsA(isA<VeilidAPIException>()));
// Delete already delete should fail
await expectLater(() async => pool.deleteRecord(rec3.key),
throwsA(isA<VeilidAPIException>()));
}
}
Future<void> testDHTRecordScopes() async {
final pool = DHTRecordPool.instance;
// Delete scope with exception should propagate exception
{
final rec = await pool.createRecord(debugName: 'test_scope 1');
await expectLater(
() async => rec.deleteScope((recd) async {
throw Exception();
}),
throwsA(isA<Exception>()));
// Set should fail
await expectLater(() async => rec.tryWriteBytes(utf8.encode('test')),
throwsA(isA<VeilidAPIException>()));
}
// Delete scope without exception
{
final rec2 = await pool.createRecord(debugName: 'test_scope 2');
try {
await rec2.deleteScope((rec2d) async {
//
});
} on Exception {
assert(false, 'should not throw');
}
await rec2.close();
await pool.deleteRecord(rec2.key);
}
// Close scope without exception
{
final rec3 = await pool.createRecord(debugName: 'test_scope 3');
try {
await rec3.scope((rec3d) async {
//
});
} on Exception {
assert(false, 'should not throw');
}
// Set should fail because scope closed it
await expectLater(() async => rec3.tryWriteBytes(utf8.encode('test')),
throwsA(isA<VeilidAPIException>()));
await pool.deleteRecord(rec3.key);
}
}
Future<void> testDHTRecordGetSet() async {
final pool = DHTRecordPool.instance;
final valdata = utf8.encode('test');
// Test get without set
{
final rec = await pool.createRecord(debugName: 'test_get_set 1');
final val = await rec.get();
await pool.deleteRecord(rec.key);
expect(val, isNull);
}
// Test set then get
{
final rec2 = await pool.createRecord(debugName: 'test_get_set 2');
expect(await rec2.tryWriteBytes(valdata), isNull);
expect(await rec2.get(), equals(valdata));
// Invalid subkey should throw
await expectLater(
() async => rec2.get(subkey: 1), throwsA(isA<VeilidAPIException>()));
await pool.deleteRecord(rec2.key);
}
// Test set then delete then open then get
{
final rec3 = await pool.createRecord(debugName: 'test_get_set 3');
expect(await rec3.tryWriteBytes(valdata), isNull);
expect(await rec3.get(), equals(valdata));
await rec3.close();
await pool.deleteRecord(rec3.key);
final rec4 =
await pool.openRecordRead(rec3.key, debugName: 'test_get_set 4');
expect(await rec4.get(), equals(valdata));
await rec4.close();
await pool.deleteRecord(rec4.key);
}
}
Future<void> testDHTRecordDeepCreateDelete() async {
final pool = DHTRecordPool.instance;
const numChildren = 20;
const numIterations = 10;
// Make root record
final recroot = await pool.createRecord(debugName: 'test_deep_create_delete');
for (var d = 0; d < numIterations; d++) {
// Make child set 1
var parent = recroot;
final children = <DHTRecord>[];
for (var n = 0; n < numChildren; n++) {
final child =
await pool.createRecord(debugName: 'deep $n', parent: parent.key);
children.add(child);
parent = child;
}
// Make child set 2
final children2 = <DHTRecord>[];
parent = recroot;
for (var n = 0; n < numChildren; n++) {
final child =
await pool.createRecord(debugName: 'deep2 $n ', parent: parent.key);
children2.add(child);
parent = child;
}
// Should fail to delete root
await expectLater(
() async => pool.deleteRecord(recroot.key), throwsA(isA<StateError>()));
// Close child set 1
await children.map((c) => c.close()).wait;
// Delete child set 1 in reverse order
for (var n = numChildren - 1; n >= 0; n--) {
await pool.deleteRecord(children[n].key);
}
// Should fail to delete root
await expectLater(
() async => pool.deleteRecord(recroot.key), throwsA(isA<StateError>()));
// Close child set 1
await children2.map((c) => c.close()).wait;
// Delete child set 2 in reverse order
for (var n = numChildren - 1; n >= 0; n--) {
await pool.deleteRecord(children2[n].key);
}
}
// Should be able to delete root now
await pool.deleteRecord(recroot.key);
}

View file

@ -3,7 +3,86 @@ import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:veilid_support/veilid_support.dart';
Future<void> testDHTShortArrayCreate() async {
// final cs = await Veilid.instance.bestCryptoSystem();
// expect(await cs.defaultSaltLength(), equals(16));
Future<void> testDHTShortArrayCreateDelete() async {
// Close before delete
{
final arr = await DHTShortArray.create(debugName: 'sa_create_delete 1');
expect(await arr.operate((r) async => r.length), isZero);
expect(arr.isOpen, isTrue);
await arr.close();
expect(arr.isOpen, isFalse);
await arr.delete();
// Operate should fail
await expectLater(() async => arr.operate((r) async => r.length),
throwsA(isA<StateError>()));
}
// Close after delete
{
final arr = await DHTShortArray.create(debugName: 'sa_create_delete 2');
await arr.delete();
// Operate should still succeed because things aren't closed
expect(await arr.operate((r) async => r.length), isZero);
await arr.close();
// Operate should fail
await expectLater(() async => arr.operate((r) async => r.length),
throwsA(isA<StateError>()));
}
// Close after delete multiple
// Okay to request delete multiple times before close
{
final arr = await DHTShortArray.create(debugName: 'sa_create_delete 3');
await arr.delete();
await arr.delete();
// Operate should still succeed because things aren't closed
expect(await arr.operate((r) async => r.length), isZero);
await arr.close();
await arr.close();
// Operate should fail
await expectLater(() async => arr.operate((r) async => r.length),
throwsA(isA<StateError>()));
}
}
Future<void> testDHTShortArrayAdd() async {
final arr = await DHTShortArray.create(debugName: 'sa_add 1');
final dataset =
Iterable<int>.generate(256).map((n) => utf8.encode('elem $n')).toList();
print('adding');
{
final (res, ok) = await arr.operateWrite((w) async {
for (var n = 0; n < dataset.length; n++) {
print('add $n');
final success = await w.tryAddItem(dataset[n]);
expect(success, isTrue);
}
});
expect(res, isNull);
expect(ok, isTrue);
}
print('get all');
{
final dataset2 = await arr.operate((r) async => r.getAllItems());
expect(dataset2, equals(dataset));
}
print('clear');
{
final (res, ok) = await arr.operateWrite((w) async => w.tryClear());
expect(res, isTrue);
expect(ok, isTrue);
}
print('get all');
{
final dataset3 = await arr.operate((r) async => r.getAllItems());
expect(dataset3, isEmpty);
}
await arr.delete();
await arr.close();
}