Skip to content

Commit 0df932e

Browse files
committed
safer file handling
1 parent fc6475f commit 0df932e

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

Assets/UXF/Scripts/DataHandling/FileSaver.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class FileSaver : LocalFileDataHander
4141

4242

4343
/// <summary>
44-
/// Starts the FileIOManager Worker thread.
44+
/// Starts the FileSaver Worker thread.
4545
/// </summary>
4646
public override void SetUp()
4747
{
@@ -71,7 +71,7 @@ public void ManageInWorker(System.Action action)
7171
{
7272
throw new System.InvalidOperationException(
7373
string.Format(
74-
"Cant add action to FileIOManager, is currently quitting. Action: {0}.{1}",
74+
"Cannot add action to FileSaver, is currently quitting. Action: {0}.{1}",
7575
action.Method.ReflectedType.FullName,
7676
action.Method.Name
7777
)
@@ -160,7 +160,7 @@ public override string HandleJSONSerializableObject(List<object> serializableObj
160160
Directory.CreateDirectory(directory);
161161
string savePath = Path.Combine(directory, string.Format("{0}.json", dataName));
162162

163-
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
163+
if (verboseDebug) Debug.LogFormat("Queuing save of file: {0}", savePath);
164164

165165
ManageInWorker(() => { File.WriteAllText(savePath, text); });
166166
return savePath;
@@ -233,7 +233,7 @@ public static string SessionNumToName(int num)
233233
}
234234

235235
/// <summary>
236-
/// Aborts the FileIOManager's thread and joins the thread to the calling thread.
236+
/// Aborts the FileSaver's thread and joins the thread to the calling thread.
237237
/// </summary>
238238
public override void CleanUp()
239239
{

Assets/UXF/Scripts/Session.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,12 @@ public void End()
642642
SaveDataTable(ppDetailsTable, "participant_details", dataType: UXFDataType.ParticipantDetails);
643643
}
644644

645-
// end DataHandler - forces completion of tasks
646-
foreach (var dataHandler in ActiveDataHandlers) dataHandler.CleanUp();
645+
// end DataHandlers - forces completion of tasks
646+
foreach (var dataHandler in ActiveDataHandlers)
647+
{
648+
try { dataHandler.CleanUp(); }
649+
catch (Exception e) { Debug.LogException(e); }
650+
}
647651

648652
try { onSessionEnd.Invoke(this); }
649653
catch (Exception e) { Debug.LogException(e); }

Assets/UXF/Tests/Editor/TestFileIOManager.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,29 @@ public void WriteManyFiles()
4343
// generate a large dictionary
4444
var dict = new Dictionary<string, object>();
4545

46-
var largeArray = new int[10000];
47-
for (int i = 0; i < largeArray.Length; i++)
48-
largeArray[i] = i;
49-
50-
dict["large_array"] = largeArray;
46+
var largeArray = new string[100];
47+
string largeString = new string('*', 50000);
5148

5249
// write lots and lots of JSON files
5350
int n = 100;
5451
string[] fpaths = new string[n];
5552
for (int i = 0; i < n; i++)
5653
{
57-
string fileName = string.Format("{0:000}", i);
54+
string fileName = string.Format("{0}", i);
5855
Debug.LogFormat("Queueing {0}", fileName);
59-
string fpath = fileSaver.HandleJSONSerializableObject(dict, experiment, ppid, sessionNum, fileName, UXFDataType.OtherSessionData);
56+
string fpath = fileSaver.HandleText(largeString, experiment, ppid, sessionNum, fileName, UXFDataType.OtherSessionData);
6057
fpaths[i] = fpath;
6158
}
6259

60+
Debug.Log("###########################################");
61+
Debug.Log("############## CLEANING UP ################");
62+
Debug.Log("###########################################");
6363
fileSaver.CleanUp();
6464

65+
Assert.Throws<System.InvalidOperationException>(() => {
66+
fileSaver.HandleText(largeString, experiment, ppid, sessionNum, "0", UXFDataType.OtherSessionData);
67+
});
68+
6569
// cleanup files
6670
foreach (var fpath in fpaths)
6771
{

Assets/UXF/Tests/Editor/TestSessionBehaviour.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ public void TestEndAfterLastTrial()
6767
Assert.AreEqual(6, lines.Length);
6868
}
6969

70+
71+
[Test]
72+
public void TestEndMultipleTimes()
73+
{
74+
(Session session, FileSaver fileSaver) = CreateSession("endmultipletimes");
75+
foreach (var trial in session.Trials)
76+
{
77+
trial.Begin();
78+
trial.End();
79+
}
80+
session.End();
81+
session.End();
82+
}
83+
7084
Tuple<Session, FileSaver> CreateSession(string ppidExtra)
7185
{
7286
GameObject gameObject = new GameObject();
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
2-
unit_test,test_behaviour_endafterlasttrial,1,1,1,1,2.747739,2.747739
3-
unit_test,test_behaviour_endafterlasttrial,1,2,1,2,2.747739,2.747739
4-
unit_test,test_behaviour_endafterlasttrial,1,3,2,1,2.747739,2.747739
5-
unit_test,test_behaviour_endafterlasttrial,1,4,2,2,2.747739,2.747739
6-
unit_test,test_behaviour_endafterlasttrial,1,5,2,3,2.747739,2.747739
2+
unit_test,test_behaviour_endafterlasttrial,1,1,1,1,1.48592,1.48592
3+
unit_test,test_behaviour_endafterlasttrial,1,2,1,2,1.48592,1.48592
4+
unit_test,test_behaviour_endafterlasttrial,1,3,2,1,1.48592,1.48592
5+
unit_test,test_behaviour_endafterlasttrial,1,4,2,2,1.48592,1.48592
6+
unit_test,test_behaviour_endafterlasttrial,1,5,2,3,1.48592,1.48592

0 commit comments

Comments
 (0)