Skip to content

Commit 800d229

Browse files
twometresteveSteve Kirkland
andauthored
Use Maze Runner idempotent commands in e2e tests (#209)
* Use Maze Runner idemopotent commands * Update Cucumber steps for idempotent commands --------- Co-authored-by: Steve Kirkland <steve.kirkland@smartbear.com>
1 parent 1b99e01 commit 800d229

File tree

2 files changed

+50
-65
lines changed

2 files changed

+50
-65
lines changed

features/fixtures/mazerunner/Assets/Scripts/Main.cs

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
public class Command
1111
{
1212
public string action;
13+
public string uuid;
1314
public string scenarioName;
1415
}
1516

@@ -33,6 +34,8 @@ public class Main : MonoBehaviour
3334

3435
private const string API_KEY = "a35a2a72bd230ac0aa0f52715bbdc6aa";
3536
private string _fixtureConfigFileName = "/fixture_config.json";
37+
private string _commandUuidFileName = "/command_uuid.txt";
38+
private static string LastCommandUuid;
3639
public static string MazeHost;
3740

3841
public ScenarioRunner ScenarioRunner;
@@ -45,12 +48,36 @@ private void Awake()
4548
public IEnumerator Start()
4649
{
4750
Log("Maze Runner app started");
51+
GetLastCommandUuid();
4852

4953
yield return GetFixtureConfig();
5054

5155
InvokeRepeating("DoRunNextMazeCommand", 0, 1);
5256
}
5357

58+
private void GetLastCommandUuid()
59+
{
60+
var uuidFilePath = Application.persistentDataPath + _commandUuidFileName;
61+
if (File.Exists(uuidFilePath))
62+
{
63+
LastCommandUuid = File.ReadAllText(uuidFilePath);
64+
}
65+
else
66+
{
67+
LastCommandUuid = "";
68+
}
69+
Log("Last command UUID is: " + LastCommandUuid);
70+
}
71+
72+
private void SetLastCommandUuid(String uuid)
73+
{
74+
Log("Setting last command UUID: " + uuid);
75+
var uuidFilePath = Application.persistentDataPath + _commandUuidFileName;
76+
File.WriteAllText(uuidFilePath, uuid);
77+
LastCommandUuid = uuid;
78+
Log("Command UUID is now: " + LastCommandUuid);
79+
}
80+
5481
private IEnumerator GetFixtureConfig()
5582
{
5683
if (Application.platform == RuntimePlatform.Android ||
@@ -73,7 +100,7 @@ private IEnumerator GetFixtureConfig()
73100
{
74101
Log("Mazerunner no fixture config found at path: " + configPath);
75102
numTries++;
76-
if(numTries == timeOut)
103+
if (numTries == timeOut)
77104
{
78105
Log("Timedout looking for config file!");
79106
}
@@ -105,60 +132,54 @@ private void DoRunNextMazeCommand()
105132

106133
IEnumerator RunNextMazeCommand()
107134
{
108-
var url = MazeHost + "/command";
109-
Log("Trying to get next mazerunner command with url: " + url);
135+
var url = MazeHost + "/idem-command?after=" + LastCommandUuid;
136+
Log("Requesting Maze Runner command from: " + url);
110137
using (UnityWebRequest request = UnityWebRequest.Get(url))
111138
{
112139
yield return request.SendWebRequest();
113-
#if UNITY_2020_1_OR_NEWER
114140
var result = request != null && request.result == UnityWebRequest.Result.Success;
115-
#else
116-
var result = request != null &&
117-
!request.isHttpError &&
118-
!request.isNetworkError;
119-
#endif
120141

121142
if (result)
122143
{
123144
var response = request.downloadHandler?.text;
124-
if (response == null || response == "null" || response == "No commands to provide" || response.Contains("noop"))
145+
if (response == null || response == "null")
125146
{
126-
147+
Log("No Maze Runner command to process at present");
127148
}
128149
else
129150
{
130151
var command = JsonUtility.FromJson<Command>(response);
131152
if (command != null)
132153
{
133-
Log("Got Action: " + command.action + " and scenario: " + command.scenarioName);
134-
if ("clear_cache".Equals(command.action))
135-
{
136-
ClearUnityCache();
137-
}
138-
else if ("run_scenario".Equals(command.action))
139-
{
140-
ScenarioRunner.RunScenario(command.scenarioName, API_KEY, MazeHost);
141-
}
142-
else if ("close_application".Equals(command.action))
154+
Log("Received Maze Runner command:\n" + response);
155+
156+
switch(command.action)
143157
{
144-
CloseFixture();
158+
case "noop":
159+
break;
160+
case "reset_uuid":
161+
SetLastCommandUuid("");
162+
break;
163+
case "clear_cache":
164+
ClearUnityCache();
165+
SetLastCommandUuid(command.uuid);
166+
break;
167+
case "run_scenario":
168+
SetLastCommandUuid(command.uuid);
169+
ScenarioRunner.RunScenario(command.scenarioName, API_KEY, MazeHost);
170+
break;
145171
}
146172
}
147173
}
148174
}
149175
else
150176
{
151-
Log("Getting next mazerunner command Failed: " + request.error);
177+
Log("Getting next Maze Runner command failed: " + request.error);
152178

153179
}
154180
}
155181
}
156182

157-
private void CloseFixture()
158-
{
159-
Application.Quit();
160-
}
161-
162183
private void ClearUnityCache()
163184
{
164185
if (Directory.Exists(Application.persistentDataPath + "/bugsnag-performance"))
@@ -173,11 +194,6 @@ private void ClearUnityCache()
173194
{
174195
ClearIOSData();
175196
}
176-
if (Application.platform != RuntimePlatform.Android &&
177-
Application.platform != RuntimePlatform.IPhonePlayer)
178-
{
179-
Invoke("CloseFixture", 0.25f);
180-
}
181197
}
182198

183199
public static void ClearIOSData()
@@ -197,8 +213,4 @@ public static void Log(string msg)
197213
catch { }
198214

199215
}
200-
201216
}
202-
203-
204-

features/steps/unity_steps.rb

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ def execute_command(action, scenario_name = '')
66
scenarioName: scenario_name
77
}
88
Maze::Server.commands.add command
9-
10-
# Ensure fixture has read the command
11-
count = 300
12-
sleep 0.1 until Maze::Server.commands.remaining.empty? || (count -= 1) < 1
13-
raise 'Test fixture did not GET /command' unless Maze::Server.commands.remaining.empty?
149
end
1510

1611
When('I clear the Bugsnag cache') do
@@ -30,36 +25,14 @@ def execute_command(action, scenario_name = '')
3025
When('I run the game in the {string} state') do |state|
3126
platform = Maze::Helper.get_current_platform
3227
case platform
33-
when 'macos'
34-
# Call executable directly rather than use open, which flakes on CI
35-
log = File.join(Dir.pwd, "#{state}-mazerunner.log")
36-
command = "#{Maze.config.app}/Contents/MacOS/Mazerunner --args -logfile #{log} > /dev/null"
37-
Maze::Runner.run_command(command, blocking: false)
38-
39-
execute_command('run_scenario', state)
40-
41-
when 'windows'
42-
win_log = File.join(Dir.pwd, "#{state}-mazerunner.log")
43-
command = "#{Maze.config.app} --args -logfile #{win_log}"
44-
Maze::Runner.run_command(command, blocking: false)
45-
28+
when 'macos','android', 'ios', 'switch', 'windows'
4629
execute_command('run_scenario', state)
47-
48-
when 'android', 'ios'
49-
execute_command('run_scenario', state)
50-
5130
when 'browser'
5231
# WebGL in a browser
5332
url = "http://localhost:#{Maze.config.port}/docs/index.html"
5433
$logger.debug "Navigating to URL: #{url}"
5534
step("I navigate to the URL \"#{url}\"")
5635
execute_command('run_scenario', state)
57-
58-
when 'switch'
59-
60-
switch_run_on_target
61-
execute_command('run_scenario', state)
62-
6336
else
6437
raise "Platform #{platform} has not been considered"
6538
end

0 commit comments

Comments
 (0)