aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/test/test_io.h
blob: 986af5f45fc45fcace287c1ae09bf23117860863 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/data/memory.h>
#include <vespa/vespalib/data/writable_memory.h>
#include <vespa/vespalib/data/input.h>
#include <vespa/vespalib/data/output.h>
#include <vespa/vespalib/data/simple_buffer.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/process/process.h>
#include <functional>

namespace vespalib::eval::test {

/**
 * Simple adapter making stdin act as an Input.
 **/
class StdIn : public Input {
private:
    bool _eof = false;
    SimpleBuffer _input;
public:
    ~StdIn() {}
    Memory obtain() override;
    Input &evict(size_t bytes) override;
};

/**
 * Simple adapter making stdout act as an Output.
 **/
class StdOut : public Output {
private:
    SimpleBuffer _output;
public:
    ~StdOut() {}
    WritableMemory reserve(size_t bytes) override;
    Output &commit(size_t bytes) override;
};

/**
 * A command run as a child process that acts as a server reading json
 * from stdin and writing json to stdout.
 **/
class ServerCmd {
private:
    Process _child;
    vespalib::string _basename;
    bool _closed;
    bool _exited;
    int _exit_code;

    void maybe_close();
    void maybe_exit();

    void dump_string(const char *prefix, const vespalib::string &str);
    void dump_message(const char *prefix, const Slime &slime);

public:
    struct capture_stderr_tag{};
    ServerCmd(vespalib::string cmd);
    ServerCmd(vespalib::string cmd, capture_stderr_tag);
    ~ServerCmd();
    Slime invoke(const Slime &req);
    vespalib::string write_then_read_all(const vespalib::string &input);
    int shutdown();
};

/**
 * Read one line at a time from an input
 **/
class LineReader {
private:
    Input &_input;
public:
    LineReader(Input &input) : _input(input) {}
    bool read_line(vespalib::string &line);
};

/**
 * Skip whitespaces from the input and return true if eof was reached.
 **/
bool look_for_eof(Input &input);

/**
 * Read from the input until eof is reached (data is discarded).
 **/
void read_until_eof(Input &input);

/**
 * Write a slime structure as compact json with a trailing newline.
 **/
void write_compact(const Slime &slime, Output &out);

/**
 * Write tests to the given output. Will write a minimal summary when
 * destructed. The current test will be flushed to the output when a
 * new test is created or right before writing the summary. The
 * 'create' function will return an object. A test may be any object
 * containing at least one field, but a test may not contain the
 * 'num_tests' field (to avoid confusion with the trailing summary)
 **/
class TestWriter {
private:
    Output &_out;
    Slime   _test;
    size_t  _num_tests;
    void maybe_write_test();
public:
    TestWriter(Output &output);
    slime::Cursor &create();
    ~TestWriter();
};

/**
 * Reads all tests from 'in' as well as the trailing summary. The
 * provided 'handle_test' function will be called for each test and
 * the 'handle_summary' function will be called once at the end. This
 * function also does some minor consistency checking.
 **/
void for_each_test(Input &in,
                   const std::function<void(Slime&)> &handle_test,
                   const std::function<void(Slime&)> &handle_summary);

} // namespace vespalib::eval::test