Skip to content

Commit

Permalink
Moved some of the tests around, and added a new http test.
Browse files Browse the repository at this point in the history
  • Loading branch information
bramp committed Feb 2, 2024
1 parent 9d42afa commit fa488ee
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 78 deletions.
77 changes: 0 additions & 77 deletions packages/pmtiles_tests/test/archive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:path/path.dart' as path;

import '../samples/headers.dart';
import 'io_helpers.dart';
import 'server_args.dart';

final client = http.Client();
late final String pmtilesUrl;
Expand Down Expand Up @@ -44,82 +43,6 @@ dynamic getReferenceMetadata(String sample) async {
return json.decoder.convert(utf8.decode(response.bodyBytes));
}

String pmtilesServingToUrl(String logline) {
return logline.replaceAllMapped(
RegExp(r'(.* Serving .* port )(\d+)( .* interface )([\d.]+)(.*)'),
(Match m) => 'http://${m[4]}:${m[2]}');
// ${m[4]} may be 0.0.0.0, which seems to allow us to connect to (on my
// mac), but I'm not sure that's valid everywhere. Maybe we replaced
// that with localhost.
}

/// Start a `pmtiles serve` instance, returning the URL its running on.
Future<String> startPmtilesServer() async {
final channel = spawnHybridUri(
'server.dart',
stayAlive: true,
message: ServerArgs(
executable: 'pmtiles',
arguments: [
'serve',
'.',

'--port', '\$port',

// Allow requests from any origin. This allows the `chrome` browser
// based tests to work.
'--cors', '*'
],
workingDirectory: 'samples',
).toJson(),
);

addTearDown(() async {
// Tell the pmtiles server to shutdown and wait for the sink to be closed.
channel.sink.add('tearDownAll');
await channel.sink.done;
});

// Get the url pmtiles server is running on.
return pmtilesServingToUrl(await channel.stream.first);
}

/// Starts a plain http server, returning the URL its running on.
Future<String> startHttpServer() async {
final channel = spawnHybridUri(
'server.dart',
stayAlive: true,
message: ServerArgs(
executable: 'http-server',
arguments: [
'.',

// Allow requests from any origin. This allows the `chrome` browser
// based tests to work.
'--cors', '*'
],
workingDirectory: 'samples',

// Needed for `env` in http-server to find `node`.
includeParentEnvironment: true,
).toJson(),
);

addTearDown(() async {
// Tell the server to shutdown and wait for the sink to be closed.
channel.sink.add('tearDownAll');
await channel.sink.done;
});

final url = await channel.stream
.firstWhere((line) => line.contains('http://127.0.0.1:'), orElse: () {
throw Exception('Failed to find available line.');
});

// Get the url server is running on.
return (url as String).trim();
}

// This is very heavy handed, but we'll run a `pmtiles server`, and makes 1000s
// of API calls comparing the reference results to the results to our library.
//
Expand Down
34 changes: 34 additions & 0 deletions packages/pmtiles_tests/test/http_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:pmtiles/pmtiles.dart';
import 'package:test/test.dart';
import 'package:http/http.dart' as http;

import 'io_helpers.dart';

void main() async {
late HttpAt at;

setUpAll(() async {
final httpUrl = await startHttpServer();

final client = http.Client();
at = HttpAt(client, Uri.parse('$httpUrl/countries-leaf.pmtiles'));
});

test('Http.ReadAt', () async {
for (final size in [1024, 1024 * 1024, 1024 * 1024 * 7]) {
final s = await at.readAt(0, size);
final bytes = await s.toBytes();

expect(bytes.length, size);
}
});

test('Http.ReadAt with offset', () async {
for (final size in [1024, 1024 * 1024, 1024 * 1024 * 7]) {
final s = await at.readAt(8978, size);
final bytes = await s.toBytes();

expect(bytes.length, size);
}
});
}
79 changes: 79 additions & 0 deletions packages/pmtiles_tests/test/io_helpers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:http/http.dart';
import 'package:pmtiles/pmtiles.dart';
import 'package:test/test.dart';

import 'server_args.dart';

/// Wrapper for a ReadAt, that counts how many requests/bytes are read.
class CountingReadAt implements ReadAt {
Expand All @@ -26,3 +29,79 @@ class CountingReadAt implements ReadAt {
bytes = 0;
}
}

String pmtilesServingToUrl(String logline) {
return logline.replaceAllMapped(
RegExp(r'(.* Serving .* port )(\d+)( .* interface )([\d.]+)(.*)'),
(Match m) => 'http://${m[4]}:${m[2]}');
// ${m[4]} may be 0.0.0.0, which seems to allow us to connect to (on my
// mac), but I'm not sure that's valid everywhere. Maybe we replaced
// that with localhost.
}

/// Start a `pmtiles serve` instance, returning the URL its running on.
Future<String> startPmtilesServer() async {
final channel = spawnHybridUri(
'server.dart',
stayAlive: true,
message: ServerArgs(
executable: 'pmtiles',
arguments: [
'serve',
'.',

'--port', '\$port',

// Allow requests from any origin. This allows the `chrome` browser
// based tests to work.
'--cors', '*'
],
workingDirectory: 'samples',
).toJson(),
);

addTearDown(() async {
// Tell the pmtiles server to shutdown and wait for the sink to be closed.
channel.sink.add('tearDownAll');
await channel.sink.done;
});

// Get the url pmtiles server is running on.
return pmtilesServingToUrl(await channel.stream.first);
}

/// Starts a plain http server, returning the URL its running on.
Future<String> startHttpServer() async {
final channel = spawnHybridUri(
'server.dart',
stayAlive: true,
message: ServerArgs(
executable: 'http-server',
arguments: [
'.',

// Allow requests from any origin. This allows the `chrome` browser
// based tests to work.
'--cors', '*'
],
workingDirectory: 'samples',

// Needed for `env` in http-server to find `node`.
includeParentEnvironment: true,
).toJson(),
);

addTearDown(() async {
// Tell the server to shutdown and wait for the sink to be closed.
channel.sink.add('tearDownAll');
await channel.sink.done;
});

final url = await channel.stream
.firstWhere((line) => line.contains('http://127.0.0.1:'), orElse: () {
throw Exception('Failed to find available line.');
});

// Get the url server is running on.
return (url as String).trim();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:test/test.dart';

import 'archive_test.dart';
import 'io_helpers.dart';

// Quis custodiet ipsos custodes?
void main() async {
Expand Down

0 comments on commit fa488ee

Please sign in to comment.