blob: d4b837245433c7338ef569ccc2fd2806d5208adb [file] [log] [blame]
Roger Meier2be7f242012-05-10 09:01:45 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Roger Meier2b1a5282012-05-11 10:12:39 +000019#include "EventLog.h"
Roger Meier2be7f242012-05-10 09:01:45 +000020
21#include <stdarg.h>
22
23using namespace std;
24using namespace apache::thrift::concurrency;
25
26namespace {
27
28void debug(const char* fmt, ...) {
29 // Comment out this return to enable debug logs from the test code.
30 return;
31
32 va_list ap;
33 va_start(ap, fmt);
34 vfprintf(stderr, fmt, ap);
35 va_end(ap);
36
37 fprintf(stderr, "\n");
38}
Roger Meier2be7f242012-05-10 09:01:45 +000039}
40
Konrad Grochowski16a23a62014-11-13 15:33:38 +010041namespace apache {
42namespace thrift {
43namespace test {
Roger Meier2be7f242012-05-10 09:01:45 +000044
45uint32_t EventLog::nextId_ = 0;
46
47#define EVENT_TYPE(value) EventType EventLog::value = #value
48EVENT_TYPE(ET_LOG_END);
49EVENT_TYPE(ET_CONN_CREATED);
50EVENT_TYPE(ET_CONN_DESTROYED);
51EVENT_TYPE(ET_CALL_STARTED);
52EVENT_TYPE(ET_CALL_FINISHED);
53EVENT_TYPE(ET_PROCESS);
54EVENT_TYPE(ET_PRE_READ);
55EVENT_TYPE(ET_POST_READ);
56EVENT_TYPE(ET_PRE_WRITE);
57EVENT_TYPE(ET_POST_WRITE);
58EVENT_TYPE(ET_ASYNC_COMPLETE);
59EVENT_TYPE(ET_HANDLER_ERROR);
60
61EVENT_TYPE(ET_CALL_INCREMENT_GENERATION);
62EVENT_TYPE(ET_CALL_GET_GENERATION);
63EVENT_TYPE(ET_CALL_ADD_STRING);
64EVENT_TYPE(ET_CALL_GET_STRINGS);
65EVENT_TYPE(ET_CALL_GET_DATA_WAIT);
66EVENT_TYPE(ET_CALL_ONEWAY_WAIT);
67EVENT_TYPE(ET_CALL_EXCEPTION_WAIT);
68EVENT_TYPE(ET_CALL_UNEXPECTED_EXCEPTION_WAIT);
69EVENT_TYPE(ET_CALL_SET_VALUE);
70EVENT_TYPE(ET_CALL_GET_VALUE);
71EVENT_TYPE(ET_WAIT_RETURN);
72
73EventLog::EventLog() {
74 id_ = nextId_++;
75 debug("New log: %d", id_);
76}
77
Konrad Grochowski16a23a62014-11-13 15:33:38 +010078void EventLog::append(EventType type,
79 uint32_t connectionId,
80 uint32_t callId,
Roger Meier2be7f242012-05-10 09:01:45 +000081 const string& message) {
82 Synchronized s(monitor_);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 debug("%d <-- %u, %u, %s \"%s\"", id_, connectionId, callId, type, message.c_str());
Roger Meier2be7f242012-05-10 09:01:45 +000084
85 Event e(type, connectionId, callId, message);
86 events_.push_back(e);
87
88 monitor_.notify();
89}
90
91Event EventLog::waitForEvent(int64_t timeout) {
92 Synchronized s(monitor_);
93
94 try {
95 while (events_.empty()) {
96 monitor_.wait(timeout);
97 }
98 } catch (TimedOutException ex) {
99 return Event(ET_LOG_END, 0, 0, "");
100 }
101
102 Event event = events_.front();
103 events_.pop_front();
104 return event;
105}
106
107Event EventLog::waitForConnEvent(uint32_t connId, int64_t timeout) {
108 Synchronized s(monitor_);
109
110 EventList::iterator it = events_.begin();
111 while (true) {
112 try {
113 // TODO: it would be nicer to honor timeout for the duration of this
114 // call, rather than restarting it for each call to wait(). It shouldn't
115 // be a big problem in practice, though.
116 while (it == events_.end()) {
117 monitor_.wait(timeout);
118 }
119 } catch (TimedOutException ex) {
120 return Event(ET_LOG_END, 0, 0, "");
121 }
122
123 if (it->connectionId == connId) {
124 Event event = *it;
125 events_.erase(it);
126 return event;
127 }
128 }
129}
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100130}
131}
132} // apache::thrift::test