• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1583599051

11 Dec 2024 09:04AM UTC coverage: 76.259% (-0.2%) from 76.43%
1583599051

push

gitlab-ci

vpodzime
fix: Cancel the previous request before scheduling a new one in HTTP resumer

The `http::Client()` class is designed to always have only one
HTTP request in progress. Thus, before scheduling a new request
using the same `http::Client` instance, cancel the previous
request to make sure everything is properly reset for the new
one.

Ticket: MEN-7810
Changelog: Fix download resuming to reset the HTTP state and
avoid repeatedly hitting the same error in case of a bad state

Signed-off-by: Vratislav Podzimek <vratislav.podzimek@northern.tech>

1 of 1 new or added line in 1 file covered. (100.0%)

55 existing lines in 11 files now uncovered.

7375 of 9671 relevant lines covered (76.26%)

11182.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.14
/tests/src/common/testing.hpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#ifndef MENDER_COMMON_TESTING
16
#define MENDER_COMMON_TESTING
17

18
#include <memory>
19
#include <string>
20

21
#include <gtest/gtest.h>
22

23
#include <common/events.hpp>
24
#include <common/http.hpp>
25

26
namespace mender {
27
namespace common {
28
namespace testing {
29

30
using namespace std;
31

32
namespace http = mender::common::http;
33

34
shared_ptr<ostream> AssertInDeathTestHelper(const char *func, const char *file, int line);
35

36
// For unknown reasons, all test assertion output is disabled inside death test sub processes. It's
37
// extremely annoying to debug, because all errors will seem to come from the hook that invoked the
38
// death test sub method, and no additional output is printed. So make our own assert which aborts
39
// instead. Return a stream so that we can pipe into it as well, like we can with the Googletest
40
// asserts. Note two things in particular:
41
//
42
// 1. The use of an empty block when expression is true. This is to prevent streaming functions from
43
//    being called in that case, since they may not work then, for example for
44
//    `ASSERT_IN_DEATH_TEST(expected_reader) << expected_reader.error()`.
45
//
46
// 2. The usage of brace-less else. This is so that we can use the rest of the line to stream into
47
//    the helper without closing it with a brace.
48
//
49
#define ASSERT_IN_DEATH_TEST(EXPR) \
50
        if (EXPR) {                    \
51
        } else                         \
52
                *::mender::common::testing::AssertInDeathTestHelper(__PRETTY_FUNCTION__, __FILE__, __LINE__)
53

54
class TemporaryDirectory {
55
public:
56
        TemporaryDirectory();
57
        ~TemporaryDirectory();
58

59
        std::string Path() const;
60

61
        void CreateSubDirectory(const string &dirname);
62

63
private:
64
        std::string path_;
65
};
66

67
// An event loop which automatically times out after a given amount of time.
68
class TestEventLoop : public mender::common::events::EventLoop {
69
public:
70
        TestEventLoop(chrono::seconds seconds = chrono::seconds(5)) :
6✔
71
                timer_ {*this} {
6✔
72
                timer_.AsyncWait(seconds, [this](error::Error err) {
6✔
UNCOV
73
                        Stop();
×
74
                        // Better to throw exception than FAIL(), since we want to escape the caller
75
                        // as well.
UNCOV
76
                        throw runtime_error("Test timed out");
×
UNCOV
77
                });
×
78
        }
6✔
79

80
private:
81
        mender::common::events::Timer timer_;
82
};
83

84
::testing::AssertionResult FileContains(const string &filename, const string &expected_content);
85
::testing::AssertionResult FileContainsExactly(
86
        const string &filename, const string &expected_content);
87
::testing::AssertionResult FileJsonEquals(const string &filename, const string &expected_content);
88
::testing::AssertionResult FilesEqual(const string &filename1, const string &filename2);
89
::testing::AssertionResult FilesNotEqual(const string &filename1, const string &filename2);
90

91
class RedirectStreamOutputs {
92
public:
93
        RedirectStreamOutputs() {
94
                cout_stream_ = cout.rdbuf(cout_string_.rdbuf());
95
                cerr_stream_ = cerr.rdbuf(cerr_string_.rdbuf());
96
        }
97
        ~RedirectStreamOutputs() {
98
                cout.rdbuf(cout_stream_);
99
                cerr.rdbuf(cerr_stream_);
100
        }
101

102
        string GetCout() const {
103
                return cout_string_.str();
104
        }
105

106
        string GetCerr() const {
107
                return cerr_string_.str();
108
        }
109

110
private:
111
        streambuf *cout_stream_;
112
        streambuf *cerr_stream_;
113
        stringstream cout_string_;
114
        stringstream cerr_string_;
115
};
116

117
class HttpFileServer {
118
public:
119
        HttpFileServer(const string &dir);
120
        ~HttpFileServer();
121

122
        const string &GetBaseUrl() const {
123
                return serve_address_;
124
        }
125

126
        void Serve(http::ExpectedIncomingRequestPtr req);
127

128
private:
129
        static const string serve_address_;
130

131
        string dir_;
132
        thread thread_;
133
        events::EventLoop loop_;
134
        http::Server server_;
135
};
136

137
} // namespace testing
138
} // namespace common
139
} // namespace mender
140

141
#endif // MENDER_COMMON_TESTING
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc