1
#include "tests/health_check_sink_server.h"
2

            
3
#include <chrono>
4
#include <functional>
5
#include <string>
6

            
7
#include "envoy/data/core/v3/health_check_event.pb.h"
8

            
9
#include "source/common/common/logger.h"
10

            
11
#include "absl/base/thread_annotations.h"
12
#include "absl/synchronization/mutex.h"
13
#include "absl/time/time.h"
14
#include "absl/types/optional.h"
15
#include "tests/uds_server.h"
16

            
17
namespace Envoy {
18

            
19
HealthCheckSinkServer::HealthCheckSinkServer(const std::string path)
20
2
    : UDSServer(path, std::bind(&HealthCheckSinkServer::msgCallback, this, std::placeholders::_1)) {
21
2
}
22

            
23
2
HealthCheckSinkServer::~HealthCheckSinkServer() = default;
24

            
25
void HealthCheckSinkServer::clear() {
26
  absl::MutexLock lock(&mutex_);
27
  events_.clear();
28
}
29

            
30
absl::optional<envoy::data::core::v3::HealthCheckEvent>
31
3
HealthCheckSinkServer::waitForEvent(std::chrono::milliseconds timeout) {
32
3
  absl::MutexLock lock(&mutex_);
33
9
  auto predicate = [this]() ABSL_SHARED_LOCKS_REQUIRED(mutex_) {
34
9
    mutex_.AssertHeld();
35
9
    return !events_.empty();
36
9
  };
37
3
  mutex_.AwaitWithTimeout(absl::Condition(&predicate), absl::Milliseconds(timeout.count()));
38
3
  auto event = events_.front();
39
3
  events_.pop_front();
40
3
  return event;
41
3
}
42

            
43
3
void HealthCheckSinkServer::msgCallback(const std::string& data) {
44
3
  envoy::data::core::v3::HealthCheckEvent event;
45
3
  if (!event.ParseFromString(data)) {
46
    ENVOY_LOG(warn, "Health check event parse failed!");
47
3
  } else {
48
3
    ENVOY_LOG(info, "Health check event: {}", event.DebugString());
49
3
    absl::MutexLock lock(&mutex_);
50
3
    events_.emplace_back(event);
51
3
  }
52
3
}
53

            
54
} // namespace Envoy