OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 #define TESTING | |
6 | |
7 #include <pthread.h> | |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
10 | |
11 #include "include/dartino_api.h" | |
12 #include "include/service_api.h" | |
13 | |
14 #include "src/shared/assert.h" // NOLINT(build/include) | |
15 #include "cc/service_one.h" // NOLINT(build/include) | |
16 #include "cc/service_two.h" // NOLINT(build/include) | |
17 | |
18 static const int kDone = 1; | |
19 | |
20 static pthread_mutex_t mutex; | |
21 static pthread_cond_t cond; | |
22 static int status = 0; | |
23 | |
24 static void ChangeStatusAndNotify(int new_status) { | |
25 pthread_mutex_lock(&mutex); | |
26 status = new_status; | |
27 pthread_cond_signal(&cond); | |
28 pthread_mutex_unlock(&mutex); | |
29 } | |
30 | |
31 static void WaitForStatus(int expected) { | |
32 pthread_mutex_lock(&mutex); | |
33 while (expected != status) pthread_cond_wait(&cond, &mutex); | |
34 pthread_mutex_unlock(&mutex); | |
35 } | |
36 | |
37 static void* DartThreadEntry(void* argv) { | |
38 const char** paths = static_cast<const char**>(argv); | |
39 DartinoSetup(); | |
40 DartinoProgram programs[2]; | |
41 int exitcodes[2]; | |
42 programs[0] = DartinoLoadSnapshotFromFile(paths[1]); | |
43 programs[1] = DartinoLoadSnapshotFromFile(paths[2]); | |
44 DartinoRunMultipleMain(2, programs, exitcodes, 0, NULL); | |
45 DartinoDeleteProgram(programs[0]); | |
46 DartinoDeleteProgram(programs[1]); | |
47 DartinoTearDown(); | |
48 ChangeStatusAndNotify(kDone); | |
49 return NULL; | |
50 } | |
51 | |
52 static void SetupMultipleSnapshotsTest(int argc, char** argv) { | |
53 pthread_mutex_init(&mutex, NULL); | |
54 pthread_cond_init(&cond, NULL); | |
55 ServiceApiSetup(); | |
56 pthread_t thread; | |
57 int result = pthread_create(&thread, NULL, DartThreadEntry, argv); | |
58 if (result != 0) { | |
59 perror("Failed to start thread"); | |
60 exit(1); | |
61 } | |
62 } | |
63 | |
64 static void TearDownMultipleSnapshotsTest() { | |
65 WaitForStatus(kDone); | |
66 ServiceApiTearDown(); | |
67 } | |
68 | |
69 static void InteractWithServices() { | |
70 ServiceOne::setup(); | |
71 ServiceTwo::setup(); | |
72 | |
73 EXPECT_EQ(10, ServiceOne::echo(5)); | |
74 EXPECT_EQ(25, ServiceTwo::echo(5)); | |
75 | |
76 ServiceTwo::tearDown(); | |
77 ServiceOne::tearDown(); | |
78 } | |
79 | |
80 int main(int argc, char** argv) { | |
81 if (argc < 3) { | |
82 printf("Usage: %s <snapshot_one> <snapshot_two>\n", argv[0]); | |
83 return 1; | |
84 } | |
85 SetupMultipleSnapshotsTest(argc, argv); | |
86 InteractWithServices(); | |
87 TearDownMultipleSnapshotsTest(); | |
88 } | |
OLD | NEW |