]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/forb/src/tests/executor/executor_test.c
77b80ee8b5cb67f4f8675068a0f816801b3fa743
[frescor/frsh-forb.git] / src / forb / src / tests / executor / executor_test.c
1 #include <forb.h>
2 #include <executor_test.h>
3 #define WVTEST_CONFIGURED
4 #include <wvtest.h>
5 #include <forb/executor.h>
6 #include <forb/object.h>
7
8 static CORBA_long add(executor_test obj, CORBA_long val, CORBA_Environment *ev)
9 {
10         int to_add = (intptr_t)forb_object_instance_data(obj);
11         return val + to_add;
12 }
13
14 static CORBA_long add_indirect(executor_test obj, executor_test indirect_obj, CORBA_long val, CORBA_Environment *ev)
15 {
16         return executor_test_add(indirect_obj, val, ev);
17 }
18
19 static const struct forb_executor_test_impl executor_test_impl = {
20         .add = add,
21         .add_indirect = add_indirect,
22 };
23
24
25 void *executor_thread(void *arg)
26 {
27         forb_executor_t *executor = arg;
28         forb_executor_run(executor);
29         return NULL;
30 }
31
32 WVTEST_MAIN("remote (inter-server) invocation")
33 {
34         forb_orb orb1, orb2;
35         fosa_thread_id_t tid;
36         executor_test testobj, remote_obj;
37         forb_executor_t executor;
38         char *str;
39         struct forb_env env;
40
41         /* Create the first FORB server */
42         WVPASS(orb1 = forb_init(NULL, NULL,
43                                 &(struct forb_init_attr){.orb_id = "server1"}));
44
45         /* This object adds 1 to the argument of add() */
46         WVPASS(testobj = forb_executor_test_new(orb1, &executor_test_impl, (void*)1));
47         WVPASSEQ(forb_executor_init(&executor), 0);
48         WVPASSEQ(forb_executor_register_object(&executor, testobj), 0);
49
50         /* Execute executor in a separate thread */
51         fosa_thread_create(&tid, NULL, executor_thread, &executor);
52
53         /* Create the second FORB server in the same process */
54         WVPASS(orb2 = forb_init(NULL, NULL,
55                                 &(struct forb_init_attr){.orb_id = "server2"}));
56
57         str = forb_object_to_string(testobj);
58         remote_obj = forb_string_to_object(orb2, str);
59
60         WVPASS(forb_object_is_local(testobj));
61         WVFAIL(forb_object_is_local(remote_obj));
62
63         /* Remote invocation of the object */
64         WVPASSEQ(executor_test_add(remote_obj, 1, &env), 2);
65         WVFAIL(forb_exception_occurred(&env));
66 }
67
68 WVTEST_MAIN("inter_thread_invocation")
69 {
70         forb_orb orb;
71         fosa_thread_id_t tid1, tid2;
72         executor_test testobj1, testobj2;
73         forb_executor_t executor1, executor2;
74         struct forb_env env;
75
76         /* Create the first FORB server */
77         WVPASS(orb = forb_init(NULL, NULL,
78                                &(struct forb_init_attr){.orb_id = "server"}));
79
80         /* This object adds 1 to the argument of add() */
81         WVPASS(testobj1 = forb_executor_test_new(orb, &executor_test_impl, (void*)1));
82         WVPASSEQ(forb_executor_init(&executor1), 0);
83         WVPASSEQ(forb_executor_register_object(&executor1, testobj1), 0);
84
85         /* This object adds 2 to the argument of add() */
86         WVPASS(testobj2 = forb_executor_test_new(orb, &executor_test_impl, (void*)2));
87         WVPASSEQ(forb_executor_init(&executor2), 0);
88         WVPASSEQ(forb_executor_register_object(&executor2, testobj2), 0);
89
90         /* Execute executors in separate threads */
91         fosa_thread_create(&tid1, NULL, executor_thread, &executor1);
92         fosa_thread_create(&tid2, NULL, executor_thread, &executor2);
93
94         WVPASS(forb_object_is_local(testobj1));
95         WVPASS(forb_object_is_local(testobj2));
96
97         /* Inter-thread invocation: application->executor */
98         WVPASSEQ(executor_test_add(testobj1, 1, &env), 2);
99         WVFAIL(forb_exception_occurred(&env));
100
101         /* Inter-thread invocation: (application->)executor->executor */
102         WVPASSEQ(executor_test_add_indirect(testobj1, testobj2, 1, &env), 3);
103         WVFAIL(forb_exception_occurred(&env));
104
105         /* Direct invocation in the same executor: (application->)executor->executor */
106         WVPASSEQ(executor_test_add_indirect(testobj1, testobj1, 1, &env), 2);
107         WVFAIL(forb_exception_occurred(&env));
108 }