Skip to content

Commit

Permalink
strategy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Jan 12, 2025
1 parent 1489c6e commit 69c1d51
Showing 1 changed file with 111 additions and 1 deletion.
112 changes: 111 additions & 1 deletion packages/ndk/test/usecases/files/blossom_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:http/http.dart' as http;

import 'package:ndk/data_layer/data_sources/http_request.dart';
import 'package:ndk/data_layer/repositories/blossom/blossom_impl.dart';
import 'package:ndk/domain_layer/usecases/files/blossom.dart';
import 'package:ndk/domain_layer/repositories/blossom.dart';
import 'package:ndk/domain_layer/usecases/files/blossom_user_server_list.dart';
import 'package:ndk/ndk.dart';

Expand Down Expand Up @@ -151,4 +151,114 @@ void main() {
expect(getResponse, throwsException);
});
});

group("blossom upload strategy tests", () {
test('Upload to first successful server only - firstSuccess', () async {
final testData = Uint8List.fromList(utf8.encode('strategy test'));

// Upload blob
final uploadResponse = await client.uploadBlob(
data: testData,
serverUrls: [
'http://dead.example.com',
'http://localhost:3001',
'http://localhost:3000',
],
strategy: UploadStrategy.firstSuccess,
);
expect(uploadResponse.first.success, true);

final sha256 = uploadResponse.first.descriptor!.sha256;

final deadServer = client.getBlob(sha256: sha256, serverUrls: [
'http://dead.example.com',
]);
expect(deadServer, throwsException);

final server1 = await client.getBlob(sha256: sha256, serverUrls: [
'http://localhost:3001',
]);

expect(utf8.decode(server1.data), equals('strategy test'));

final server2 = client.getBlob(sha256: sha256, serverUrls: [
'http://localhost:3000',
]);

expect(server2, throwsException);
});

test('Upload to first successful server only - mirrorAfterSuccess',
() async {
final myData = "strategy test mirrorAfterSuccess";
final testData = Uint8List.fromList(utf8.encode(myData));

// Upload blob
final uploadResponse = await client.uploadBlob(
data: testData,
serverUrls: [
'http://dead.example.com',
'http://localhost:3001',
'http://localhost:3000',
],
strategy: UploadStrategy.mirrorAfterSuccess,
);
expect(uploadResponse.first.success, true);

final sha256 = uploadResponse.first.descriptor!.sha256;

final deadServer = client.getBlob(sha256: sha256, serverUrls: [
'http://dead.example.com',
]);
expect(deadServer, throwsException);

final server1 = await client.getBlob(sha256: sha256, serverUrls: [
'http://localhost:3001',
]);

expect(utf8.decode(server1.data), equals(myData));

final server2 = await client.getBlob(sha256: sha256, serverUrls: [
'http://localhost:3000',
]);

expect(utf8.decode(server2.data), equals(myData));
});

test('Upload to first successful server only - allSimultaneous', () async {
final myData = "strategy test allSimultaneous";
final testData = Uint8List.fromList(utf8.encode(myData));

// Upload blob
final uploadResponse = await client.uploadBlob(
data: testData,
serverUrls: [
'http://dead.example.com',
'http://localhost:3001',
'http://localhost:3000',
],
strategy: UploadStrategy.allSimultaneous,
);
expect(uploadResponse.first.success, true);

final sha256 = uploadResponse.first.descriptor!.sha256;

final deadServer = client.getBlob(sha256: sha256, serverUrls: [
'http://dead.example.com',
]);
expect(deadServer, throwsException);

final server1 = await client.getBlob(sha256: sha256, serverUrls: [
'http://localhost:3001',
]);

expect(utf8.decode(server1.data), equals(myData));

final server2 = await client.getBlob(sha256: sha256, serverUrls: [
'http://localhost:3000',
]);

expect(utf8.decode(server2.data), equals(myData));
});
});
}

0 comments on commit 69c1d51

Please sign in to comment.