Skip to content

Commit

Permalink
Fix QueueServerConfiguration parsing from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Alekseev committed Aug 10, 2020
1 parent 8a2a22a commit ab902bc
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,10 @@ let package = Package(
dependencies: [
"AutomaticTermination",
"AutomaticTerminationTestHelpers",
"Deployer",
"DistDeployer",
"LocalQueueServerRunner",
"LoggingSetup",
"ProcessControllerTestHelpers",
"QueueCommunicationTestHelpers",
"QueueModels",
Expand All @@ -809,6 +811,7 @@ let package = Package(
"RemotePortDeterminer",
"RemotePortDeterminerTestHelpers",
"ScheduleStrategy",
"Sentry",
"SocketModels",
"TemporaryStuff",
"TestHelpers",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum AutomaticTerminationPolicy: Codable, CustomStringConvertible {
public enum AutomaticTerminationPolicy: Codable, CustomStringConvertible, Equatable {
/// Will trigger termination after being idle for the given amout of time.
case afterBeingIdle(period: TimeInterval)

Expand Down
36 changes: 36 additions & 0 deletions Sources/LocalQueueServerRunner/QueueServerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ public struct QueueServerConfiguration: Decodable {
self.workerSpecificConfigurations = workerSpecificConfigurations
}

private enum CodingKeys: String, CodingKey {
case analyticsConfiguration
case checkAgainTimeInterval
case queueServerDeploymentDestination
case queueServerTerminationPolicy
case workerDeploymentDestinations
case workerSpecificConfigurations
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

let analyticsConfiguration = try container.decode(AnalyticsConfiguration.self, forKey: .analyticsConfiguration)
let checkAgainTimeInterval = try container.decode(TimeInterval.self, forKey: .checkAgainTimeInterval)
let queueServerDeploymentDestination = try container.decode(DeploymentDestination.self, forKey: .queueServerDeploymentDestination)
let queueServerTerminationPolicy = try container.decode(AutomaticTerminationPolicy.self, forKey: .queueServerTerminationPolicy)
let workerDeploymentDestinations = try container.decode([DeploymentDestination].self, forKey: .workerDeploymentDestinations)
let workerSpecificConfigurations = Dictionary(
uniqueKeysWithValues: try container.decode(
[String: WorkerSpecificConfiguration].self,
forKey: .workerSpecificConfigurations
).map { key, value in
(WorkerId(key), value)
}
)

self.init(
analyticsConfiguration: analyticsConfiguration,
checkAgainTimeInterval: checkAgainTimeInterval,
queueServerDeploymentDestination: queueServerDeploymentDestination,
queueServerTerminationPolicy: queueServerTerminationPolicy,
workerDeploymentDestinations: workerDeploymentDestinations,
workerSpecificConfigurations: workerSpecificConfigurations
)
}

public func workerConfiguration(
workerSpecificConfiguration: WorkerSpecificConfiguration,
payloadSignature: PayloadSignature
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import QueueModels

public struct WorkerSpecificConfiguration: Decodable {
public struct WorkerSpecificConfiguration: Decodable, Equatable {
public let numberOfSimulators: UInt

public init(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import AutomaticTermination
import Deployer
import Foundation
import LocalQueueServerRunner
import LoggingSetup
import QueueModels
import Sentry
import SocketModels
import XCTest

final class QueueServerConfigurationTests: XCTestCase {
func test___parsing() throws {
let data = """
{
"analyticsConfiguration": {"graphiteConfiguration": {"socketAddress": "host:123", "metricPrefix": "graphite.prefix"}, "sentryConfiguration": {"dsn": "sentry.dsn"}},
"workerSpecificConfigurations": {
"worker_1": {"numberOfSimulators": 1},
"worker_2": {"numberOfSimulators": 2},
"worker_3": {"numberOfSimulators": 3}
},
"queueServerDeploymentDestination": {"host": "queue", "port": 22, "username": "q_user", "password": "q_pass", "remoteDeploymentPath": "/remote/queue/depl/path"},
"queueServerTerminationPolicy": {"caseId": "stayAlive"},
"checkAgainTimeInterval": 22,
"workerDeploymentDestinations": [
{"host": "host", "port": 1, "username": "username", "password": "password", "remoteDeploymentPath": "/remote/deployment/path"}
]
}
""".data(using: .utf8)!

let config = try JSONDecoder().decode(QueueServerConfiguration.self, from: data)
XCTAssertEqual(
config.analyticsConfiguration,
AnalyticsConfiguration(
graphiteConfiguration: GraphiteConfiguration(socketAddress: SocketAddress(host: "host", port: 123), metricPrefix: "graphite.prefix"),
sentryConfiguration: SentryConfiguration(dsn: URL(string: "sentry.dsn")!)
)
)
XCTAssertEqual(
config.workerSpecificConfigurations,
[
WorkerId("worker_1"): WorkerSpecificConfiguration(numberOfSimulators: 1),
WorkerId("worker_2"): WorkerSpecificConfiguration(numberOfSimulators: 2),
WorkerId("worker_3"): WorkerSpecificConfiguration(numberOfSimulators: 3),
]
)
XCTAssertEqual(
config.queueServerDeploymentDestination,
DeploymentDestination(host: "queue", port: 22, username: "q_user", password: "q_pass", remoteDeploymentPath: "/remote/queue/depl/path")
)
XCTAssertEqual(
config.queueServerTerminationPolicy,
AutomaticTerminationPolicy.stayAlive
)
XCTAssertEqual(
config.checkAgainTimeInterval,
22
)
XCTAssertEqual(
config.workerDeploymentDestinations,
[
DeploymentDestination(host: "host", port: 1, username: "username", password: "password", remoteDeploymentPath: "/remote/deployment/path")
]
)
}
}

0 comments on commit ab902bc

Please sign in to comment.