| 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* arg) { | |
| 38 const char* path = static_cast<const char*>(arg); | |
| 39 DartinoSetup(); | |
| 40 DartinoProgram program = DartinoLoadSnapshotFromFile(path); | |
| 41 if (DartinoRunMain(program, 0, NULL) != 0) { | |
| 42 FATAL1("Failed to run snapshot: %s\n", path); | |
| 43 } | |
| 44 DartinoDeleteProgram(program); | |
| 45 DartinoTearDown(); | |
| 46 ChangeStatusAndNotify(kDone); | |
| 47 return NULL; | |
| 48 } | |
| 49 | |
| 50 static void SetupMultipleSnapshotsTest(int argc, char** argv) { | |
| 51 pthread_mutex_init(&mutex, NULL); | |
| 52 pthread_cond_init(&cond, NULL); | |
| 53 ServiceApiSetup(); | |
| 54 pthread_t thread; | |
| 55 int result = pthread_create(&thread, NULL, DartThreadEntry, argv[1]); | |
| 56 if (result != 0) { | |
| 57 perror("Failed to start thread"); | |
| 58 exit(1); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 static void TearDownMultipleSnapshotsTest() { | |
| 63 WaitForStatus(kDone); | |
| 64 ServiceApiTearDown(); | |
| 65 } | |
| 66 | |
| 67 static void InteractWithServices() { | |
| 68 ServiceOne::setup(); | |
| 69 ServiceTwo::setup(); | |
| 70 | |
| 71 EXPECT_EQ(10, ServiceOne::echo(5)); | |
| 72 EXPECT_EQ(25, ServiceTwo::echo(5)); | |
| 73 | |
| 74 ServiceTwo::tearDown(); | |
| 75 ServiceOne::tearDown(); | |
| 76 } | |
| 77 | |
| 78 int main(int argc, char** argv) { | |
| 79 if (argc < 2) { | |
| 80 printf("Usage: %s <snapshot>\n", argv[0]); | |
| 81 return 1; | |
| 82 } | |
| 83 SetupMultipleSnapshotsTest(argc, argv); | |
| 84 InteractWithServices(); | |
| 85 TearDownMultipleSnapshotsTest(); | |
| 86 } | |
| OLD | NEW |