Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 1 | /* |
| 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 Meier | 2b1a528 | 2012-05-11 10:12:39 +0000 | [diff] [blame] | 19 | #include "EventLog.h" |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 20 | |
| 21 | #include <stdarg.h> |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame^] | 22 | #include <stdlib.h> |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace std; |
| 25 | using namespace apache::thrift::concurrency; |
| 26 | |
| 27 | namespace { |
| 28 | |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame^] | 29 | // Define environment variable DEBUG_EVENTLOG to enable debug logging |
| 30 | // ex: $ DEBUG_EVENTLOG=1 processor_test |
| 31 | static const char * DEBUG_EVENTLOG = getenv("DEBUG_EVENTLOG"); |
| 32 | |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 33 | void debug(const char* fmt, ...) { |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame^] | 34 | if (DEBUG_EVENTLOG) { |
| 35 | va_list ap; |
| 36 | va_start(ap, fmt); |
| 37 | vfprintf(stderr, fmt, ap); |
| 38 | va_end(ap); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 39 | |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame^] | 40 | fprintf(stderr, "\n"); |
| 41 | } |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 42 | } |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 45 | namespace apache { |
| 46 | namespace thrift { |
| 47 | namespace test { |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 48 | |
| 49 | uint32_t EventLog::nextId_ = 0; |
| 50 | |
| 51 | #define EVENT_TYPE(value) EventType EventLog::value = #value |
| 52 | EVENT_TYPE(ET_LOG_END); |
| 53 | EVENT_TYPE(ET_CONN_CREATED); |
| 54 | EVENT_TYPE(ET_CONN_DESTROYED); |
| 55 | EVENT_TYPE(ET_CALL_STARTED); |
| 56 | EVENT_TYPE(ET_CALL_FINISHED); |
| 57 | EVENT_TYPE(ET_PROCESS); |
| 58 | EVENT_TYPE(ET_PRE_READ); |
| 59 | EVENT_TYPE(ET_POST_READ); |
| 60 | EVENT_TYPE(ET_PRE_WRITE); |
| 61 | EVENT_TYPE(ET_POST_WRITE); |
| 62 | EVENT_TYPE(ET_ASYNC_COMPLETE); |
| 63 | EVENT_TYPE(ET_HANDLER_ERROR); |
| 64 | |
| 65 | EVENT_TYPE(ET_CALL_INCREMENT_GENERATION); |
| 66 | EVENT_TYPE(ET_CALL_GET_GENERATION); |
| 67 | EVENT_TYPE(ET_CALL_ADD_STRING); |
| 68 | EVENT_TYPE(ET_CALL_GET_STRINGS); |
| 69 | EVENT_TYPE(ET_CALL_GET_DATA_WAIT); |
| 70 | EVENT_TYPE(ET_CALL_ONEWAY_WAIT); |
| 71 | EVENT_TYPE(ET_CALL_EXCEPTION_WAIT); |
| 72 | EVENT_TYPE(ET_CALL_UNEXPECTED_EXCEPTION_WAIT); |
| 73 | EVENT_TYPE(ET_CALL_SET_VALUE); |
| 74 | EVENT_TYPE(ET_CALL_GET_VALUE); |
| 75 | EVENT_TYPE(ET_WAIT_RETURN); |
| 76 | |
| 77 | EventLog::EventLog() { |
| 78 | id_ = nextId_++; |
| 79 | debug("New log: %d", id_); |
| 80 | } |
| 81 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 82 | void EventLog::append(EventType type, |
| 83 | uint32_t connectionId, |
| 84 | uint32_t callId, |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 85 | const string& message) { |
| 86 | Synchronized s(monitor_); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 87 | debug("%d <-- %u, %u, %s \"%s\"", id_, connectionId, callId, type, message.c_str()); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 88 | |
| 89 | Event e(type, connectionId, callId, message); |
| 90 | events_.push_back(e); |
| 91 | |
| 92 | monitor_.notify(); |
| 93 | } |
| 94 | |
| 95 | Event EventLog::waitForEvent(int64_t timeout) { |
| 96 | Synchronized s(monitor_); |
| 97 | |
| 98 | try { |
| 99 | while (events_.empty()) { |
| 100 | monitor_.wait(timeout); |
| 101 | } |
| 102 | } catch (TimedOutException ex) { |
| 103 | return Event(ET_LOG_END, 0, 0, ""); |
| 104 | } |
| 105 | |
| 106 | Event event = events_.front(); |
| 107 | events_.pop_front(); |
| 108 | return event; |
| 109 | } |
| 110 | |
| 111 | Event EventLog::waitForConnEvent(uint32_t connId, int64_t timeout) { |
| 112 | Synchronized s(monitor_); |
| 113 | |
| 114 | EventList::iterator it = events_.begin(); |
| 115 | while (true) { |
| 116 | try { |
| 117 | // TODO: it would be nicer to honor timeout for the duration of this |
| 118 | // call, rather than restarting it for each call to wait(). It shouldn't |
| 119 | // be a big problem in practice, though. |
| 120 | while (it == events_.end()) { |
| 121 | monitor_.wait(timeout); |
| 122 | } |
| 123 | } catch (TimedOutException ex) { |
| 124 | return Event(ET_LOG_END, 0, 0, ""); |
| 125 | } |
| 126 | |
| 127 | if (it->connectionId == connId) { |
| 128 | Event event = *it; |
| 129 | events_.erase(it); |
| 130 | return event; |
| 131 | } |
| 132 | } |
| 133 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } // apache::thrift::test |