Skip to content

Commit 3d4fa43

Browse files
committed
improved testing
1 parent 605af95 commit 3d4fa43

File tree

14 files changed

+111
-18
lines changed

14 files changed

+111
-18
lines changed

Assets/UXF/Examples/Basic/BasicExample.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ Prefab:
269269
- target: {fileID: 224546371281550106, guid: 0a3b6392f04558844bd340e68ced1ff9,
270270
type: 2}
271271
propertyPath: m_SizeDelta.y
272-
value: 75
272+
value: 0
273273
objectReference: {fileID: 0}
274274
- target: {fileID: 224546371281550106, guid: 0a3b6392f04558844bd340e68ced1ff9,
275275
type: 2}

Assets/UXF/Scripts/FileIOManager.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,11 @@ void Update()
5252

5353
/// <summary>
5454
/// Adds a new command to a queue which is executed in a separate worker thread when it is available.
55+
/// Warning: The Unity Engine API is not thread safe, so do not attempt to put any Unity commands here.
5556
/// </summary>
5657
/// <param name="command"></param>
5758
public void ManageInWorker(System.Action action)
5859
{
59-
if (debug)
60-
Debug.LogFormat("Managing action: {0}.{1}", action.Method.ReflectedType.FullName, action.Method.Name);
6160

6261
if (quitting)
6362
{
@@ -75,9 +74,16 @@ public void ManageInWorker(System.Action action)
7574

7675
void Worker()
7776
{
77+
if (debug)
78+
Debug.Log("Started worker thread");
79+
7880
// performs FileIO tasks in seperate thread
7981
foreach (var action in bq)
8082
{
83+
84+
if (debug && action != null)
85+
Debug.LogFormat("Managing action: {0}.{1}", action.Method.ReflectedType.FullName, action.Method.Name);
86+
8187
try
8288
{
8389
action.Invoke();
@@ -206,14 +212,16 @@ void ManageInMain()
206212

207213
void OnDestroy()
208214
{
209-
Quit();
215+
End();
210216
}
211217

212218
/// <summary>
213219
/// Aborts the FileIOManager's thread.
214220
/// </summary>
215-
public void Quit()
221+
public void End()
216222
{
223+
if (debug)
224+
Debug.Log("Joining FileIOManagerThread");
217225
quitting = true;
218226
t.Join();
219227
}

Assets/UXF/Scripts/CSVReader/Tests.meta renamed to Assets/UXF/Scripts/Tests.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using System.Collections.Generic;
5+
using NUnit.Framework;
6+
using System.Collections;
7+
8+
namespace UXF.Tests
9+
{
10+
11+
public class TestFileIOManager
12+
{
13+
14+
string path = "example_output/fileiomanager_test";
15+
FileIOManager fileIOManager;
16+
17+
[SetUp]
18+
public void SetUp()
19+
{
20+
var gameObject = new GameObject();
21+
fileIOManager = gameObject.AddComponent<FileIOManager>();
22+
fileIOManager.debug = true;
23+
24+
}
25+
26+
27+
[TearDown]
28+
public void TearDown()
29+
{
30+
GameObject.DestroyImmediate(fileIOManager.gameObject);
31+
}
32+
33+
34+
[Test]
35+
public void WriteManyFiles()
36+
{
37+
fileIOManager.Begin();
38+
39+
// generate a large dictionary
40+
var dict = new Dictionary<string, object>();
41+
42+
var largeArray = new int[10000];
43+
for (int i = 0; i < largeArray.Length; i++)
44+
largeArray[i] = i;
45+
46+
dict["large_array"] = largeArray;
47+
48+
49+
if (!System.IO.Directory.Exists(path))
50+
System.IO.Directory.CreateDirectory(path);
51+
52+
53+
// write lots of JSON files
54+
int n = 100;
55+
for (int i = 0; i < n; i++)
56+
{
57+
string fileName = System.IO.Path.Combine(path, string.Format("{0:000}.json", i));
58+
Debug.LogFormat("Queueing {0}", fileName);
59+
fileIOManager.ManageInWorker(() => fileIOManager.WriteJson(fileName, dict));
60+
}
61+
62+
fileIOManager.End();
63+
64+
// cleanup files
65+
var files = System.IO.Directory.GetFiles(path, "*.json");
66+
foreach (var file in files)
67+
{
68+
System.IO.File.Delete(file);
69+
}
70+
}
71+
}
72+
73+
}

Assets/UXF/Scripts/Tests/Editor/TestFileIOManager.cs.meta

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UXF/Scripts/CSVReader/Tests/Editor/TestSettings.cs renamed to Assets/UXF/Scripts/Tests/Editor/TestSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
using System.Collections;
77
using System.Collections.Generic;
88

9-
namespace UXF.Tests {
9+
namespace UXF.Tests
10+
{
1011

1112
public class TestSettings {
1213

Assets/UXF/Scripts/CSVReader/Tests/Editor/TestTrials.cs renamed to Assets/UXF/Scripts/Tests/Editor/TestTrials.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
using System.Collections.Generic;
88
using System.IO;
99

10-
namespace UXF.Tests {
10+
namespace UXF.Tests
11+
{
1112

1213
public class TestTrials
1314
{
@@ -52,7 +53,7 @@ public void SetUp()
5253
public void TearDown()
5354
{
5455
session.End();
55-
fileIOManager.Quit();
56+
fileIOManager.End();
5657
GameObject.DestroyImmediate(gameObject);
5758
}
5859

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
timestamp,log_type,message
2-
19.82911,Log,Managing action: UXF.Session+<WriteDictToSessionFolder>c__AnonStorey2.<>m__0
3-
19.9862,Log,Managing action: UXF.Session+<SaveResults>c__AnonStorey3.<>m__0
4-
19.9862,Log,Managing action: UXF.SessionLogger+<Finalise>c__AnonStorey0.<>m__0
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
experiment,ppid,session_num,trial_num,block_num,trial_num_in_block,start_time,end_time,observation
2-
unit_test,test_trials,1,1,1,1,19.9862,19.9862,1
3-
unit_test,test_trials,1,2,1,2,19.9862,19.9862,2
4-
unit_test,test_trials,1,3,2,1,19.9862,19.9862,3
5-
unit_test,test_trials,1,4,2,2,19.9862,19.9862,4
6-
unit_test,test_trials,1,5,2,3,19.9862,19.9862,5
2+
unit_test,test_trials,1,1,1,1,0.9902837,0.9902837,1
3+
unit_test,test_trials,1,2,1,2,0.9902837,0.9902837,2
4+
unit_test,test_trials,1,3,2,1,0.9902837,0.9902837,3
5+
unit_test,test_trials,1,4,2,2,0.9902837,0.9902837,4
6+
unit_test,test_trials,1,5,2,3,0.9902837,0.9902837,5

0 commit comments

Comments
 (0)