ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
accumulator_spec.h
1 
7 #include "../src/ceptr.h"
8 #include "../src/accumulator.h"
9 #include <sys/dir.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 
15 void _testReceptorClockAddExpectation(Receptor *r);
16 void testAccBootStrap() {
17  spec_is_ptr_equal(G_vm,NULL);
18 
19  // test first boot before any initialization has occurred
20 
21  // first boot should create a data directory
22  char *dname = "tmp/test_vm";
23  struct stat st = {0};
24 
25  system("rm -r tmp/test_vm"); // cleanup from previous test runs
26 
27  _a_boot(dname);
28  spec_is_equal(stat(dname, &st),0); // does after boot
29 
30  spec_is_str_equal(t2s(G_vm->r->root),"(RECEPTOR_INSTANCE (INSTANCE_OF:SYS_RECEPTOR) (CONTEXT_NUM:0) (PARENT_CONTEXT_NUM:-1) (RECEPTOR_STATE (FLUX (DEFAULT_ASPECT (EXPECTATIONS) (SIGNALS))) (PENDING_SIGNALS) (PENDING_RESPONSES) (CONVERSATIONS) (RECEPTOR_ELAPSED_TIME:0)))");
31 
32  // new clock receptor should be instantiated and active
33  spec_is_equal(G_vm->active_receptor_count,1);
34  Receptor *clock = G_vm->active_receptors[0].r;
35  spec_is_str_equal(t2s(clock->root),"(RECEPTOR_INSTANCE (INSTANCE_OF:CLOCK_RECEPTOR) (CONTEXT_NUM:4) (PARENT_CONTEXT_NUM:0) (RECEPTOR_STATE (FLUX (DEFAULT_ASPECT (EXPECTATIONS (EXPECTATION (CARRIER:tell_time) (PATTERN (SEMTREX_SYMBOL_LITERAL (SEMTREX_SYMBOL:CLOCK_TELL_TIME))) (ACTION:respond with current time) (PARAMS) (END_CONDITIONS (UNLIMITED)))) (SIGNALS))) (PENDING_SIGNALS) (PENDING_RESPONSES) (CONVERSATIONS) (RECEPTOR_ELAPSED_TIME:0)))");
36 
37  // now add an expectation in the clock
38  T *tick = __r_make_tick();
39  // _testReceptorClockAddExpectation(clock);
40 
41  // sleep for a bit, which should allow the clock to run for a second
42  // and reduce the tick expectation to the tick it received
43  //sleep(1);
44 
45  // @todo, this is broken since weve changed how the clock receptor works...
46  //spec_is_str_equal(_td(clock,tick),_td(clock,_t_child(clock->q->completed->context->run_tree,1)));
47 
48 
49  // __r_kill(clock);
50 
51  // clone the clock for later comparison
52  T *clk_flux = _t_clone(clock->flux);
53 
54  // now shut down the vm
55  _a_shut_down();
56  spec_is_ptr_equal(G_vm,NULL);
57 
58  // boot again
59  _a_boot(dname);
60 
61  // verify that clock with planted expectation and it's accumulated data are re-instantiated at boot
62  //__r_dump_instances(G_vm->r);
63 
64  Xaddr x = {CLOCK_RECEPTOR,1};
65  T *ct = _r_get_instance(G_vm->r,x);
66  spec_is_false(ct == NULL);
67  /* Receptor *cr = __r_get_receptor(ct); */
68 
69  /* char buf1[1000]; */
70  /* __td(cr,clk_flux,buf1); */
71 
72  /* // @todo comparison fails here because we can't control time!! sometimes it work */
73  /* // depending on threads and what ran when.. grr */
74  /* // spec_is_str_equal(_t2s(&cr->defs,cr->flux),"buf1"); */
75  /* // spec_is_str_equal(_t2s(&cr->defs,cr->flux),buf1); */
76  /* spec_is_symbol_equal(G_vm->r,_t_symbol(cr->root),CLOCK_RECEPTOR); */
77  /* spec_is_str_equal(t2s(cr->root),"(RECEPTOR_INSTANCE (CONTEXT_NUM:4) (PARENT_CONTEXT_NUM:0) (RECEPTOR_STATE (FLUX (DEFAULT_ASPECT (EXPECTATIONS (EXPECTATION (CARRIER:CLOCK_TELL_TIME) (PATTERN (SEMTREX_SYMBOL_LITERAL (SEMTREX_SYMBOL:CLOCK_TELL_TIME))) (ACTION:respond with current time) (PARAMS) (END_CONDITIONS (UNLIMITED)))) (SIGNALS))) (PENDING_SIGNALS) (PENDING_RESPONSES) (RECEPTOR_ELAPSED_TIME:0)))"); */
78  /* spec_is_equal(G_vm->active_receptor_count,1); */
79 
80  // @todo and that they run appropriately
81  _a_shut_down();
82  _t_free(clk_flux);
83  _t_free(tick);
84 }
85 
86 void testAccInstances() {
87  Instances i = NULL;
88  T *t = _t_newi(0,TEST_INT_SYMBOL,1);
89 
90  Xaddr x = _a_new_instance(&i,t);
91  spec_is_ptr_equal(t,_a_get_instance(&i,x));
92 
93  t = _t_newi(0,TEST_INT_SYMBOL,2);
94  _a_set_instance(&i,x,t);
95  spec_is_ptr_equal(t,_a_get_instance(&i,x));
96 
97  t = _t_new_str(0,TEST_STR_SYMBOL,"fish");
98  x = _a_new_instance(&i,t);
99 
100  t = _t_newi(0,TEST_INT_SYMBOL,3);
101  x = _a_new_instance(&i,t);
102 
103  t = _t_newi(0,TEST_INT_SYMBOL,4);
104  _a_new_instance(&i,t);
105  _a_delete_instance(&i,x);
106 
107  t = _a_get_instance(&i,x);
108  spec_is_ptr_equal(t,NULL);
109 
110  spec_is_str_equal(t2s(i),"(INSTANCE_STORE (INSTANCES (SYMBOL_INSTANCES:TEST_INT_SYMBOL (TEST_INT_SYMBOL:2) (DELETED_INSTANCE) (TEST_INT_SYMBOL:4)) (SYMBOL_INSTANCES:TEST_STR_SYMBOL (TEST_STR_SYMBOL:fish))))");
111  _a_free_instances(&i);
112 }
113 
114 void testAccGetInstances() {
115  Instances i = NULL;
116  T *t;
117  Xaddr x;
118 
119  t = _t_newi(0,TEST_INT_SYMBOL,1);
120  x = _a_new_instance(&i,t);
121  t = _t_newi(0,TEST_INT_SYMBOL,2);
122  x = _a_new_instance(&i,t);
123  t = _t_newi(0,TEST_INT_SYMBOL,3);
124  x = _a_new_instance(&i,t);
125  t = _t_newi(0,TEST_INT_SYMBOL,4);
126  _a_new_instance(&i,t);
127 
128  spec_is_str_equal(t2s(i),"(INSTANCE_STORE (INSTANCES (SYMBOL_INSTANCES:TEST_INT_SYMBOL (TEST_INT_SYMBOL:1) (TEST_INT_SYMBOL:2) (TEST_INT_SYMBOL:3) (TEST_INT_SYMBOL:4))))");
129 
130  _a_delete_instance(&i,x);
131 
132  t = _t_new_root(ITERATION_DATA);
133  _a_get_instances(&i,TEST_INT_SYMBOL,t);
134  spec_is_str_equal(t2s(t),"(ITERATION_DATA (TEST_INT_SYMBOL:1) (TEST_INT_SYMBOL:2) (TEST_INT_SYMBOL:4))");
135  _t_free(t);
136 
137  _a_free_instances(&i);
138 
139 }
140 
141 void testAccPersistInstances() {
142  Instances i = NULL;
143 
144  T *it = _t_newi(0,TEST_INT_SYMBOL,1);
145  _a_new_instance(&i,it);
146 
147  it = _t_newi(0,TEST_INT_SYMBOL,2);
148  Xaddr z = _a_new_instance(&i,it);
149 
150  T *t = _t_new_root(PARAMS);
151  _t_newi(t,TEST_INT_SYMBOL,314);
152  Xaddr x = _a_new_instance(&i,t);
153 
154  T *ht = _makeTestHTTPRequestTree(); // GET /groups/5/users.json?sort_by=last_name?page=2 HTTP/1.0
155  T *htc = _t_clone(ht);
156  Xaddr y = _a_new_instance(&i,ht);
157 
158  S *s = __a_serialize_instances(&i);
159  _a_free_instances(&i);
160  __a_unserialize_instances(G_sem,&i,s);
161 
162  spec_is_str_equal(t2s(_a_get_instance(&i,x)),"(PARAMS (TEST_INT_SYMBOL:314))");
163  spec_is_str_equal(t2s(_a_get_instance(&i,y)),t2s(htc));
164  spec_is_str_equal(t2s(_a_get_instance(&i,z)),"(TEST_INT_SYMBOL:2)");
165 
166  _t_free(htc);
167  _a_free_instances(&i);
168  free(s);
169 }
170 
171 void testAccToken() {
172  Instances i = NULL;
173  T *t,*token1,*token2,*d1,*d2;
174  Xaddr x,xx;
175 
176  t = _t_newi(0,TEST_INT_SYMBOL,1);
177  x = _a_new_instance(&i,t);
178 
179  d1 = _t_newi(0,TEST_INT_SYMBOL,314); // dependency 1
180  d2 = _t_newi(0,TEST_INT_SYMBOL,123); // dependency 2
181  // generate a token for the xaddr with each dependency
182  token1 = _a_gen_token(&i,x,d1);
183  token2 = _a_gen_token(&i,x,d2);
184 
185  spec_is_str_equal(t2s(i),"(INSTANCE_STORE (INSTANCES (SYMBOL_INSTANCES:TEST_INT_SYMBOL (TEST_INT_SYMBOL:1))) (INSTANCE_TOKENS (LAST_TOKEN:2) (INSTANCE_TOKEN:1 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:-1641288256)) (INSTANCE_TOKEN:2 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:1282387645))))");
186 
187  // test getting back xaddrs from tokens and their dependency
188  xx = _a_get_token_xaddr(&i,token1,d1);
189  spec_is_xaddr_equal(G_sem,x,xx);
190  xx = _a_get_token_xaddr(&i,token2,d2);
191  spec_is_xaddr_equal(G_sem,x,xx);
192 
193  // test that getting an xaddr from a token with an incorrect dependency fails
194  xx = _a_get_token_xaddr(&i,token1,d2);
195  spec_is_true(is_null_xaddr(xx));
196  xx = _a_get_token_xaddr(&i,token2,d1);
197  spec_is_true(is_null_xaddr(xx));
198 
199  // test adding a dependency to a token
200  T *d3 = _t_newi(0,TEST_INT_SYMBOL,999); // dependency 3
201  xx = _a_get_token_xaddr(&i,token1,d3);
202  spec_is_true(is_null_xaddr(xx));
203  _a_add_dependency(&i,token1,d3);
204  char *with_two_dependencies = "(INSTANCE_STORE (INSTANCES (SYMBOL_INSTANCES:TEST_INT_SYMBOL (TEST_INT_SYMBOL:1))) (INSTANCE_TOKENS (LAST_TOKEN:2) (INSTANCE_TOKEN:1 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:-1641288256) (DEPENDENCY_HASH:-226474859)) (INSTANCE_TOKEN:2 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:1282387645))))";
205  spec_is_str_equal(t2s(i),with_two_dependencies);
206  // check that dependency isn't added in twice
207  _a_add_dependency(&i,token1,d3);
208  spec_is_str_equal(t2s(i),with_two_dependencies);
209 
210  xx = _a_get_token_xaddr(&i,token1,d3);
211  spec_is_xaddr_equal(G_sem,x,xx);
212  xx = _a_get_token_xaddr(&i,token2,d3);
213  spec_is_true(is_null_xaddr(xx));
214 
215 
216  // test deleting a dependency
217  _a_delete_dependency(&i,token1,d3);
218  spec_is_str_equal(t2s(i),"(INSTANCE_STORE (INSTANCES (SYMBOL_INSTANCES:TEST_INT_SYMBOL (TEST_INT_SYMBOL:1))) (INSTANCE_TOKENS (LAST_TOKEN:2) (INSTANCE_TOKEN:1 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:-1641288256)) (INSTANCE_TOKEN:2 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:1282387645))))");
219  xx = _a_get_token_xaddr(&i,token1,d3);
220  spec_is_true(is_null_xaddr(xx));
221 
222  // test deleting a token
223  _a_delete_token(&i,token1);
224  spec_is_str_equal(t2s(i),"(INSTANCE_STORE (INSTANCES (SYMBOL_INSTANCES:TEST_INT_SYMBOL (TEST_INT_SYMBOL:1))) (INSTANCE_TOKENS (LAST_TOKEN:2) (INSTANCE_TOKEN:2 (TOKEN_XADDR:TEST_INT_SYMBOL.1) (DEPENDENCY_HASH:1282387645))))");
225 
226  _t_free(token1);
227  _t_free(token2);
228  _t_free(d1);
229  _t_free(d2);
230  _t_free(d3);
231  _a_free_instances(&i);
232 
233 }
234 
235 void testAccumulator() {
236  struct stat st = {0};
237  char *temp_dir = "tmp";
238  if (stat(temp_dir, &st) == -1) {
239  mkdir(temp_dir,0700);
240  }
241 
242  testAccBootStrap();
243  testAccInstances();
244  testAccGetInstances();
245  testAccPersistInstances();
246  testAccToken();
247 }
T * _t_new_root(Symbol symbol)
Definition: tree.c:160
Definition: ceptr_types.h:114
Definition: ceptr_types.h:68
T * _t_clone(T *t)
Definition: tree.c:589
void _a_shut_down()
Definition: accumulator.c:134
T * flux
pointer for quick access to the flux
Definition: ceptr_types.h:243
T * root
RECEPTOR_INSTANCE semantic tree.
Definition: ceptr_types.h:238
void _a_boot(char *dir_path)
Definition: accumulator.c:60
ActiveReceptor active_receptors[MAX_ACTIVE_RECEPTORS]
pointer to array that holds all currently active receptors
Definition: vmhost.h:45
T * _makeTestHTTPRequestTree()
[makeTestHTTPRequestTree]
Definition: http_example.h:26
T * _r_get_instance(Receptor *r, Xaddr x)
Definition: receptor.c:379
Receptor * r
Receptor data for this vm host.
Definition: vmhost.h:43
void _t_free(T *t)
Definition: tree.c:526