-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestPrevalence.js
156 lines (116 loc) · 5.3 KB
/
TestPrevalence.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var directory = new Date().getTime() + ".prevalence";
var initialState = {"state": "initial root"};
var prevalence = require('./Prevalence.js').factory(directory, initialState);
prevalence.open(function opened() {
console.log("Prevalence opened.");
assertEquals("initial root", prevalence.root.state);
prevalence.execute({
fields: { key: "value"},
execute: function assertTransactionWithFields(root, executionTime) {
return this.fields.key;
}
}, function (results) {
assertEquals("value", results, "assertTransactionWithFields");
});
prevalence.execute({
execute: function assertTransactionReturnValue(root, executionTime) {
return "2";
}
}, function (results) {
assertEquals("2", results, "assertTransactionReturnValue");
});
prevalence.execute({
execute: function assertTransactionWithoutFields(root, executionTime) {
return "1";
}
}, function (results) {
assertEquals("1", results, "assertTransactionWithoutFields");
});
prevalence.execute({
execute: function (root, executionTime) {
return "unnamed transaction function";
}
}, function (results) {
assertEquals("unnamed transaction function", results, "unnamed transaction function");
});
prevalence.execute({
execute: function assertUpdateState(root, executionTime) {
root.state = "updated";
}
}, function (results) {
assertEquals("updated", prevalence.root.state, "assertTransactionWithFields");
assertEquals("initial root", initialState.state, "initial state should never change, root is a clone of it!");
});
prevalence.execute({
execute: function setStartedTimeInRoot(root, executionTime) {
return root.started = executionTime;
}
}, function (started) {
assertEquals(prevalence.root.started, started, "Started does not equals");
assertEquals("updated", prevalence.root.state, "state does not match");
prevalence.end();
console.log("Prevalence closed. Re-opening 1...");
prevalence = require('./Prevalence.js').factory(directory, initialState);
prevalence.open(function opened() {
console.log("Prevalence re-opened 1.");
assertEquals("updated", prevalence.root.state, "state does not match");
assertEquals(prevalence.root.started, started, "Started does not equals after re-open and loading journal");
prevalence.execute({
execute: function assertUpdateState(root, executionTime) {
root.state = "updated 2";
}
}, function (results) {
assertEquals("updated 2", prevalence.root.state, "assertTransactionWithFields");
assertEquals("initial root", initialState.state, "initial state should never change, root is a clone of it!");
prevalence.end();
console.log("Prevalence closed. Re-opening 2...");
prevalence = require('./Prevalence.js').factory(directory, initialState);
prevalence.open(function opened() {
console.log("Prevalence re-opened 2.");
assertEquals("updated 2", prevalence.root.state, "state does not match");
assertEquals(prevalence.root.started, started, "Started does not equals after re-open and loading journal");
prevalence.takeSnapshot(function tookSnapshot() {
console.log("Prevalence snapshot written.");
prevalence.end();
console.log("Prevalence closed. Re-opening 3...");
prevalence = require('./Prevalence.js').factory(directory, initialState);
prevalence.open(function opened() {
console.log("Prevalence re-opened. 3");
assertEquals("updated 2", prevalence.root.state, "state does not match");
assertEquals(prevalence.root.started, started, "Started does not equals after re-open and loading snapshot");
prevalence.execute({
execute: function assertCreateSecondJournal(root, executionTime) {
root.state = "second journal created";
}
}, function (results) {
assertEquals("second journal created", prevalence.root.state, "state does not match");
assertEquals("initial root", initialState.state);
prevalence.end();
console.log("Prevalence closed. Re-opening 4...");
prevalence = require('./Prevalence.js').factory(directory, initialState);
prevalence.open(function opened() {
console.log("Prevalence re-opened 4.");
assertEquals("second journal created", prevalence.root.state, "state does not match");
assertEquals(prevalence.root.started, started, "Started does not equals after re-open and loading snapshot and loading a second journal");
prevalence.end();
console.log("Test over!");
});
});
});
});
});
});
});
});
}
);
function assertTrue(a, message) {
if (!a) {
throw "Expected true but was " + a + ". " + message;
}
}
function assertEquals(a, b, message) {
if (a !== b) {
throw "Expected " + a + " but was " + b + ". " + message;
}
}