1
#pragma once
2

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

            
7
#include "test/test_common/utility.h"
8

            
9
#include "absl/base/thread_annotations.h"
10
#include "absl/synchronization/mutex.h"
11
#include "absl/types/optional.h"
12
#include "cilium/api/accesslog.pb.h"
13
#include "tests/uds_server.h"
14

            
15
namespace Envoy {
16

            
17
class AccessLogServer : public UDSServer {
18
public:
19
  AccessLogServer(const std::string path);
20
  ~AccessLogServer();
21

            
22
  void clear();
23
  absl::optional<::cilium::LogEntry>
24
  waitForMessage(::cilium::EntryType entry_type,
25
                 std::chrono::milliseconds timeout = TestUtility::DefaultTimeout);
26

            
27
  template <typename P>
28
64
  bool expectRequestTo(P&& pred, std::chrono::milliseconds timeout = TestUtility::DefaultTimeout) {
29
64
    auto maybe_entry = waitForMessage(::cilium::EntryType::Request, timeout);
30
64
    if (maybe_entry.has_value()) {
31
64
      return pred(maybe_entry.value());
32
64
    }
33
    return false;
34
64
  }
35

            
36
  template <typename P>
37
66
  bool expectResponseTo(P&& pred, std::chrono::milliseconds timeout = TestUtility::DefaultTimeout) {
38
66
    auto maybe_entry = waitForMessage(::cilium::EntryType::Response, timeout);
39
66
    if (maybe_entry.has_value()) {
40
66
      return pred(maybe_entry.value());
41
66
    }
42
    return false;
43
66
  }
44

            
45
  template <typename P>
46
48
  bool expectDeniedTo(P&& pred, std::chrono::milliseconds timeout = TestUtility::DefaultTimeout) {
47
48
    auto maybe_entry = waitForMessage(::cilium::EntryType::Denied, timeout);
48
48
    if (maybe_entry.has_value()) {
49
48
      return pred(maybe_entry.value());
50
48
    }
51
    return false;
52
48
  }
53

            
54
private:
55
  void msgCallback(const std::string& data);
56

            
57
  absl::Mutex mutex_;
58
  std::vector<::cilium::LogEntry> messages_ ABSL_GUARDED_BY(mutex_);
59
};
60

            
61
} // namespace Envoy