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