-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from bugsnag/karl-automatic-mazerunner-fetch
Automatically fetch mazerunner commands rather than waiting for a button press
- Loading branch information
Showing
25 changed files
with
474 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
Feature: Automatic send triggers | ||
|
||
Scenario: BackgroundForegroundScenario | ||
Given I run "BackgroundForegroundScenario" | ||
And I send the app to the background for 2 seconds | ||
And I wait for 1 span | ||
Then the trace "Content-Type" header equals "application/json" | ||
* the trace "Bugsnag-Span-Sampling" header equals "1:1" | ||
* the trace "Bugsnag-Sent-At" header matches the regex "^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$" | ||
* every span field "name" equals "BackgroundForegroundScenario" | ||
* every span field "spanId" matches the regex "^[A-Fa-f0-9]{16}$" | ||
* every span field "traceId" matches the regex "^[A-Fa-f0-9]{32}$" | ||
* every span field "kind" equals 1 | ||
* every span field "startTimeUnixNano" matches the regex "^[0-9]+$" | ||
* every span field "endTimeUnixNano" matches the regex "^[0-9]+$" | ||
* every span bool attribute "bugsnag.app.in_foreground" is false | ||
# Disabled per https://smartbear.atlassian.net/browse/PLAT-11232 | ||
#Scenario: BackgroundForegroundScenario | ||
# Given I run "BackgroundForegroundScenario" | ||
# And I send the app to the background for 2 seconds | ||
# And I wait for 1 span | ||
# Then the trace "Content-Type" header equals "application/json" | ||
# * the trace "Bugsnag-Span-Sampling" header equals "1:1" | ||
# * the trace "Bugsnag-Sent-At" header matches the regex "^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$" | ||
# * every span field "name" equals "BackgroundForegroundScenario" | ||
# * every span field "spanId" matches the regex "^[A-Fa-f0-9]{16}$" | ||
# * every span field "traceId" matches the regex "^[A-Fa-f0-9]{32}$" | ||
# * every span field "kind" equals 1 | ||
# * every span field "startTimeUnixNano" matches the regex "^[0-9]+$" | ||
# * every span field "endTimeUnixNano" matches the regex "^[0-9]+$" | ||
# * every span bool attribute "bugsnag.app.in_foreground" is false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// CommandReaderThread.swift | ||
// iOSTestApp | ||
// | ||
// Created by Karl Stenerud on 16.11.23. | ||
// Copyright © 2023 Bugsnag. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import os | ||
|
||
class CommandReaderThread: Thread { | ||
var fixtureConfig: FixtureConfig | ||
var commandReceiver: CommandReceiver | ||
|
||
init(fixtureConfig: FixtureConfig, commandReceiver: CommandReceiver) { | ||
self.fixtureConfig = fixtureConfig | ||
self.commandReceiver = commandReceiver | ||
} | ||
|
||
override func main() { | ||
while true { | ||
if self.commandReceiver.canReceiveCommand() { | ||
receiveNextCommand() | ||
} else { | ||
logDebug("A command is already in progress, waiting 1 second more...") | ||
} | ||
Thread.sleep(forTimeInterval: 1) | ||
} | ||
} | ||
|
||
func receiveNextCommand() { | ||
let fetchTask = CommandFetchTask(url: fixtureConfig.commandURL) | ||
fetchTask.start() | ||
|
||
while true { | ||
switch fetchTask.state { | ||
case CommandFetchState.success: | ||
commandReceiver.receiveCommand(command: fetchTask.command!) | ||
return | ||
case CommandFetchState.fetching: | ||
logDebug("Command fetch server hasn't responded yet, waiting 1 second more...") | ||
Thread.sleep(forTimeInterval: 1) | ||
break | ||
case CommandFetchState.failed: | ||
logInfo("Command fetch request failed. Trying again...") | ||
Thread.sleep(forTimeInterval: 1) | ||
fetchTask.start() | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
enum CommandFetchState { | ||
case failed, fetching, success | ||
} | ||
|
||
class CommandFetchTask { | ||
var url: URL | ||
var state = CommandFetchState.failed | ||
var command: MazeRunnerCommand? | ||
|
||
init(url: URL) { | ||
self.url = url | ||
} | ||
|
||
func start() { | ||
logInfo("Fetching next command from \(url)") | ||
state = CommandFetchState.fetching | ||
let request = URLRequest(url: url) | ||
let task = URLSession.shared.dataTask(with: request) { data, response, error in | ||
if let data = data { | ||
let decoder = JSONDecoder() | ||
decoder.keyDecodingStrategy = .convertFromSnakeCase | ||
do { | ||
let command = try decoder.decode(MazeRunnerCommand.self, from: data) | ||
logInfo("Command fetched and decoded") | ||
self.command = command; | ||
self.state = CommandFetchState.success | ||
} catch { | ||
self.state = CommandFetchState.failed | ||
let dataAsString = String(data: data, encoding: .utf8) | ||
logError("Failed to fetch command: Invalid Response from \(String(describing: self.url)): [\(String(describing: dataAsString))]: Error is: \(error)") | ||
} | ||
} else if let error = error { | ||
self.state = CommandFetchState.failed | ||
logError("Failed to fetch command: HTTP Request to \(String(describing: self.url)) failed: \(error)") | ||
} | ||
} | ||
task.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.