-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1187 from atsign-foundation/delete_expired_keys
feat: schedule job that removes expired keys
- Loading branch information
Showing
6 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/at_client/test/delete_expired_keys_task_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:at_client/at_client.dart'; | ||
import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; | ||
import 'package:at_persistence_secondary_server/src/keystore/hive_keystore.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class MockCommitLog extends Mock implements ClientAtCommitLog {} | ||
|
||
void main() { | ||
var storagePath = '${Directory.current.path}/test/hive'; | ||
String atsign = '@expiry_test'; | ||
var commitLog = MockCommitLog(); | ||
|
||
group('validate behaviour of scheduled deleteExpiredKeys task', () { | ||
when(() => commitLog.commit(any(), CommitOp.UPDATE_ALL)) | ||
.thenAnswer((_) => Future.value(1)); | ||
|
||
when(() => commitLog.commit(any(), CommitOp.DELETE)) | ||
.thenAnswer((_) => Future.value(2)); | ||
|
||
setUp(() async { | ||
AtClientImpl.atClientInstanceMap.remove(atsign); | ||
await setUpStorage(atsign, storagePath, commitLog); | ||
}); | ||
|
||
test('verify that delete expired keys task removes expired keys', () async { | ||
String key1 = 'public:expired_key_1.test$atsign'; | ||
AtMetaData metadata = AtMetaData()..ttl = 1; | ||
AtData data = AtData() | ||
..data = 'data_key_1' | ||
..metaData = metadata; | ||
await getKeyStore(atsign)?.put(key1, data); | ||
|
||
String key2 = 'public:expired_key_2.test$atsign'; | ||
data = AtData() | ||
..data = 'data_key_2' | ||
..metaData = metadata; | ||
await getKeyStore(atsign)?.put(key2, data); | ||
|
||
String key3 = 'public:unexpired_key_3.test$atsign'; | ||
metadata.ttl = 1000000; | ||
data = AtData() | ||
..data = 'data_key_3' | ||
..metaData = metadata; | ||
await getKeyStore(atsign)?.put(key3, data); | ||
|
||
await SecondaryPersistenceStoreFactory.getInstance() | ||
.getSecondaryPersistenceStore(atsign) | ||
?.getSecondaryKeyStore() | ||
?.deleteExpiredKeys(); | ||
|
||
int exceptionCatchCount = 0; | ||
try { | ||
await getKeyStore(atsign)?.get(key1); | ||
} on Exception catch (e) { | ||
expect(e.toString().contains('$key1 does not exist in keystore'), true); | ||
exceptionCatchCount++; | ||
} | ||
|
||
try { | ||
await getKeyStore(atsign)?.get(key2); | ||
} on Exception catch (e) { | ||
expect(e.toString().contains('$key2 does not exist in keystore'), true); | ||
exceptionCatchCount++; | ||
} | ||
|
||
expect((await getKeyStore(atsign)?.get(key3))?.data, 'data_key_3'); | ||
expect(exceptionCatchCount, | ||
2); // this counter maintains the count of how many exceptions have been caught | ||
}); | ||
|
||
tearDown(() async { | ||
await tearDownLocalStorage(storagePath); | ||
}); | ||
}); | ||
} | ||
|
||
HiveKeystore? getKeyStore(String atsign) { | ||
return SecondaryPersistenceStoreFactory.getInstance() | ||
.getSecondaryPersistenceStore(atsign) | ||
?.getSecondaryKeyStore(); | ||
} | ||
|
||
Future<void> setUpStorage( | ||
String atsign, String storagePath, ClientAtCommitLog commitLog) async { | ||
var manager = SecondaryPersistenceStoreFactory.getInstance() | ||
.getSecondaryPersistenceStore(atsign); | ||
await manager?.getHivePersistenceManager()?.init(storagePath); | ||
manager?.getSecondaryKeyStore()?.commitLog = commitLog; | ||
} | ||
|
||
Future<void> tearDownLocalStorage(storageDir) async { | ||
try { | ||
var isExists = await Directory(storageDir).exists(); | ||
if (isExists) { | ||
Directory(storageDir).deleteSync(recursive: true); | ||
} | ||
} catch (e, st) { | ||
print('local_secondary_test.dart: exception / error in tearDown: $e, $st'); | ||
} | ||
} |