Skip to content

Commit 855cddb

Browse files
Merge pull request #1 from isaachier/ankit-varma10-master
Add test case for new exception handling code
2 parents 57a6b4b + 83f2605 commit 855cddb

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

src/jaegertracing/UDPTransport.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Uber Technologies, Inc.
2+
* Copyright (c) 2017-2018 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -115,15 +115,19 @@ int UDPTransport::flush()
115115
batch.__set_spans(_spanBuffer);
116116

117117
try {
118-
_client->emitBatch(batch);
118+
_client->emitBatch(batch);
119119
} catch (const std::system_error& ex) {
120-
std::ostringstream oss;
121-
oss << "Could not send span " << ex.what() << ", code=" << ex.code().value();
122-
throw Transport::Exception(oss.str(), _spanBuffer.size());
123-
} catch (const std::exception &ex) {
124-
std::ostringstream oss;
125-
oss << "Could not send span " << ex.what();
126-
throw Transport::Exception(oss.str(), _spanBuffer.size());
120+
std::ostringstream oss;
121+
oss << "Could not send span " << ex.what()
122+
<< ", code=" << ex.code().value();
123+
throw Transport::Exception(oss.str(), _spanBuffer.size());
124+
} catch (const std::exception& ex) {
125+
std::ostringstream oss;
126+
oss << "Could not send span " << ex.what();
127+
throw Transport::Exception(oss.str(), _spanBuffer.size());
128+
} catch (...) {
129+
throw Transport::Exception("Could not send span, unknown error",
130+
_spanBuffer.size());
127131
}
128132

129133
resetBuffers();

src/jaegertracing/UDPTransport.h

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class UDPTransport : public Transport {
3636

3737
void close() override { _client->close(); }
3838

39+
protected:
40+
void setClient(std::unique_ptr<utils::UDPClient>&& client)
41+
{
42+
_client = std::move(client);
43+
}
44+
3945
private:
4046
void resetBuffers()
4147
{

src/jaegertracing/UDPTransportTest.cpp

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Uber Technologies, Inc.
2+
* Copyright (c) 2017-2018 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,50 @@
2323
#include "jaegertracing/utils/ErrorUtil.h"
2424

2525
namespace jaegertracing {
26+
namespace {
27+
28+
class MockUDPClient : public utils::UDPClient {
29+
public:
30+
enum class ExceptionType { kSystemError, kException, kString };
31+
32+
MockUDPClient(const net::IPAddress& serverAddr,
33+
int maxPacketSize,
34+
ExceptionType type)
35+
: UDPClient(serverAddr, maxPacketSize)
36+
, _type(type)
37+
{
38+
}
39+
40+
private:
41+
void emitBatch(const thrift::Batch& batch) override
42+
{
43+
switch (_type) {
44+
case ExceptionType::kSystemError:
45+
throw std::system_error();
46+
case ExceptionType::kException:
47+
throw std::exception();
48+
default:
49+
assert(_type == ExceptionType::kString);
50+
throw "error";
51+
}
52+
}
53+
54+
ExceptionType _type;
55+
};
56+
57+
class MockUDPTransport : public UDPTransport {
58+
public:
59+
MockUDPTransport(const net::IPAddress& ip,
60+
int maxPacketSize,
61+
MockUDPClient::ExceptionType type)
62+
: UDPTransport(ip, maxPacketSize)
63+
{
64+
setClient(std::unique_ptr<utils::UDPClient>(
65+
new MockUDPClient(ip, maxPacketSize, type)));
66+
}
67+
};
68+
69+
} // anonymous namespace
2670

2771
TEST(UDPTransport, testManyMessages)
2872
{
@@ -40,4 +84,25 @@ TEST(UDPTransport, testManyMessages)
4084
}
4185
}
4286

87+
TEST(UDPTransport, testExceptions)
88+
{
89+
const auto handle = testutils::TracerUtil::installGlobalTracer();
90+
const auto tracer =
91+
std::static_pointer_cast<const Tracer>(opentracing::Tracer::Global());
92+
93+
Span span(tracer);
94+
span.SetOperationName("test");
95+
96+
const MockUDPClient::ExceptionType exceptionTypes[] = {
97+
MockUDPClient::ExceptionType::kSystemError,
98+
MockUDPClient::ExceptionType::kException,
99+
MockUDPClient::ExceptionType::kString
100+
};
101+
for (auto type : exceptionTypes) {
102+
MockUDPTransport sender(net::IPAddress(), 0, type);
103+
sender.append(span);
104+
ASSERT_THROW(sender.flush(), Transport::Exception);
105+
}
106+
}
107+
43108
} // namespace jaegertracing

src/jaegertracing/utils/UDPClient.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Uber Technologies, Inc.
2+
* Copyright (c) 2017-2018 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ class UDPClient : public agent::thrift::AgentIf {
5454
if (static_cast<int>(size) > _maxPacketSize) {
5555
std::ostringstream oss;
5656
oss << "Data does not fit within one UDP packet"
57-
"; size "
57+
", size "
5858
<< size << ", max " << _maxPacketSize << ", spans "
5959
<< batch.spans.size();
6060
throw std::logic_error(oss.str());

0 commit comments

Comments
 (0)