Skip to content

Commit 2722fcd

Browse files
authored
Sort agent IDs naturally (#392)
1 parent 55b0ccb commit 2722fcd

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

app_dart/lib/src/model/appengine/agent.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Agent extends Model {
1919
this.agentId,
2020
this.healthCheckTimestamp,
2121
this.isHealthy,
22-
this.isHidden,
22+
this.isHidden = false,
2323
this.capabilities,
2424
this.healthDetails,
2525
this.authToken,

app_dart/lib/src/request_handlers/get_status.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:collection/collection.dart';
78
import 'package:gcloud/db.dart';
89
import 'package:meta/meta.dart';
910

@@ -37,6 +38,7 @@ class GetStatus extends RequestHandler<Body> {
3738
final DatastoreService datastore = datastoreProvider();
3839
final Query<Agent> agentQuery = datastore.db.query<Agent>()..order('agentId');
3940
final List<Agent> agents = await agentQuery.run().where(_isVisible).toList();
41+
agents.sort((Agent a, Agent b) => compareAsciiLowerCaseNatural(a.agentId, b.agentId));
4042

4143
return Body.forJson(<String, dynamic>{
4244
'Statuses': statuses,

app_dart/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ packages:
121121
source: hosted
122122
version: "3.2.0"
123123
collection:
124-
dependency: transitive
124+
dependency: "direct main"
125125
description:
126126
name: collection
127127
url: "https://pub.dartlang.org"

app_dart/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ environment:
1212
dependencies:
1313
appengine: ^0.10.0
1414
dbcrypt: ^1.0.0
15+
collection: ^1.14.12
1516
crypto: ^2.0.6
1617
fixnum: ^0.10.9
1718
gcloud: ^0.6.3
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
import 'dart:typed_data';
7+
8+
import 'package:cocoon_service/src/model/appengine/agent.dart';
9+
import 'package:cocoon_service/src/request_handlers/get_status.dart';
10+
import 'package:cocoon_service/src/request_handling/body.dart';
11+
import 'package:cocoon_service/src/service/build_status_provider.dart';
12+
import 'package:cocoon_service/src/service/datastore.dart';
13+
import 'package:test/test.dart';
14+
15+
import '../src/datastore/fake_cocoon_config.dart';
16+
import '../src/datastore/fake_datastore.dart';
17+
import '../src/service/fake_build_status_provider.dart';
18+
19+
void main() {
20+
group('GetStatus', () {
21+
FakeConfig config;
22+
FakeDatastoreDB db;
23+
FakeBuildStatusProvider buildStatusProvider;
24+
GetStatus handler;
25+
26+
Future<Object> decodeHandlerBody() async {
27+
final Body body = await handler.get();
28+
return utf8.decoder.bind(body.serialize()).transform(json.decoder).single;
29+
}
30+
31+
setUp(() {
32+
config = FakeConfig();
33+
buildStatusProvider = FakeBuildStatusProvider(commitStatuses: <CommitStatus>[]);
34+
db = FakeDatastoreDB();
35+
handler = GetStatus(
36+
config,
37+
datastoreProvider: () => DatastoreService(db: db),
38+
buildStatusProvider: buildStatusProvider,
39+
);
40+
});
41+
42+
test('no statuses or agents', () async {
43+
final Map<String, dynamic> result = await decodeHandlerBody();
44+
expect(result['Statuses'], isEmpty);
45+
expect(result['AgentStatuses'], isEmpty);
46+
});
47+
48+
test('reports agents', () async {
49+
final Agent linux1 = Agent(agentId: 'linux1');
50+
final Agent mac1 = Agent(agentId: 'mac1');
51+
final Agent linux100 = Agent(agentId: 'linux100');
52+
final Agent linux5 = Agent(agentId: 'linux5');
53+
final Agent windows1 = Agent(agentId: 'windows1', isHidden: true);
54+
55+
final List<Agent> reportedAgents = <Agent>[
56+
linux1,
57+
mac1,
58+
linux100,
59+
linux5,
60+
windows1,
61+
];
62+
63+
db.addOnQuery<Agent>((Iterable<Agent> agents) => reportedAgents);
64+
final Map<String, dynamic> result = await decodeHandlerBody();
65+
66+
expect(result['Statuses'], isEmpty);
67+
68+
final List<dynamic> expectedOrderedAgents = <dynamic>[
69+
linux1.toJson(),
70+
linux5.toJson(),
71+
linux100.toJson(),
72+
mac1.toJson(),
73+
];
74+
75+
expect(result['AgentStatuses'], equals(expectedOrderedAgents));
76+
});
77+
});
78+
}

0 commit comments

Comments
 (0)