Skip to content

Commit

Permalink
Sort agent IDs naturally (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmagman authored Aug 28, 2019
1 parent 55b0ccb commit 2722fcd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app_dart/lib/src/model/appengine/agent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Agent extends Model {
this.agentId,
this.healthCheckTimestamp,
this.isHealthy,
this.isHidden,
this.isHidden = false,
this.capabilities,
this.healthDetails,
this.authToken,
Expand Down
2 changes: 2 additions & 0 deletions app_dart/lib/src/request_handlers/get_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';

import 'package:collection/collection.dart';
import 'package:gcloud/db.dart';
import 'package:meta/meta.dart';

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

return Body.forJson(<String, dynamic>{
'Statuses': statuses,
Expand Down
2 changes: 1 addition & 1 deletion app_dart/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ packages:
source: hosted
version: "3.2.0"
collection:
dependency: transitive
dependency: "direct main"
description:
name: collection
url: "https://pub.dartlang.org"
Expand Down
1 change: 1 addition & 0 deletions app_dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ environment:
dependencies:
appengine: ^0.10.0
dbcrypt: ^1.0.0
collection: ^1.14.12
crypto: ^2.0.6
fixnum: ^0.10.9
gcloud: ^0.6.3
Expand Down
78 changes: 78 additions & 0 deletions app_dart/test/request_handlers/get_status_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:typed_data';

import 'package:cocoon_service/src/model/appengine/agent.dart';
import 'package:cocoon_service/src/request_handlers/get_status.dart';
import 'package:cocoon_service/src/request_handling/body.dart';
import 'package:cocoon_service/src/service/build_status_provider.dart';
import 'package:cocoon_service/src/service/datastore.dart';
import 'package:test/test.dart';

import '../src/datastore/fake_cocoon_config.dart';
import '../src/datastore/fake_datastore.dart';
import '../src/service/fake_build_status_provider.dart';

void main() {
group('GetStatus', () {
FakeConfig config;
FakeDatastoreDB db;
FakeBuildStatusProvider buildStatusProvider;
GetStatus handler;

Future<Object> decodeHandlerBody() async {
final Body body = await handler.get();
return utf8.decoder.bind(body.serialize()).transform(json.decoder).single;
}

setUp(() {
config = FakeConfig();
buildStatusProvider = FakeBuildStatusProvider(commitStatuses: <CommitStatus>[]);
db = FakeDatastoreDB();
handler = GetStatus(
config,
datastoreProvider: () => DatastoreService(db: db),
buildStatusProvider: buildStatusProvider,
);
});

test('no statuses or agents', () async {
final Map<String, dynamic> result = await decodeHandlerBody();
expect(result['Statuses'], isEmpty);
expect(result['AgentStatuses'], isEmpty);
});

test('reports agents', () async {
final Agent linux1 = Agent(agentId: 'linux1');
final Agent mac1 = Agent(agentId: 'mac1');
final Agent linux100 = Agent(agentId: 'linux100');
final Agent linux5 = Agent(agentId: 'linux5');
final Agent windows1 = Agent(agentId: 'windows1', isHidden: true);

final List<Agent> reportedAgents = <Agent>[
linux1,
mac1,
linux100,
linux5,
windows1,
];

db.addOnQuery<Agent>((Iterable<Agent> agents) => reportedAgents);
final Map<String, dynamic> result = await decodeHandlerBody();

expect(result['Statuses'], isEmpty);

final List<dynamic> expectedOrderedAgents = <dynamic>[
linux1.toJson(),
linux5.toJson(),
linux100.toJson(),
mac1.toJson(),
];

expect(result['AgentStatuses'], equals(expectedOrderedAgents));
});
});
}

0 comments on commit 2722fcd

Please sign in to comment.