Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for new exception handling code #1

Merged
merged 2 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/jaegertracing/UDPTransport.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Uber Technologies, Inc.
* Copyright (c) 2017-2018 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,15 +115,19 @@ int UDPTransport::flush()
batch.__set_spans(_spanBuffer);

try {
_client->emitBatch(batch);
_client->emitBatch(batch);
} catch (const std::system_error& ex) {
std::ostringstream oss;
oss << "Could not send span " << ex.what() << ", code=" << ex.code().value();
throw Transport::Exception(oss.str(), _spanBuffer.size());
} catch (const std::exception &ex) {
std::ostringstream oss;
oss << "Could not send span " << ex.what();
throw Transport::Exception(oss.str(), _spanBuffer.size());
std::ostringstream oss;
oss << "Could not send span " << ex.what()
<< ", code=" << ex.code().value();
throw Transport::Exception(oss.str(), _spanBuffer.size());
} catch (const std::exception& ex) {
std::ostringstream oss;
oss << "Could not send span " << ex.what();
throw Transport::Exception(oss.str(), _spanBuffer.size());
} catch (...) {
throw Transport::Exception("Could not send span, unknown error",
_spanBuffer.size());
}

resetBuffers();
Expand Down
6 changes: 6 additions & 0 deletions src/jaegertracing/UDPTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class UDPTransport : public Transport {

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

protected:
void setClient(std::unique_ptr<utils::UDPClient>&& client)
{
_client = std::move(client);
}

private:
void resetBuffers()
{
Expand Down
67 changes: 66 additions & 1 deletion src/jaegertracing/UDPTransportTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Uber Technologies, Inc.
* Copyright (c) 2017-2018 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,50 @@
#include "jaegertracing/utils/ErrorUtil.h"

namespace jaegertracing {
namespace {

class MockUDPClient : public utils::UDPClient {
public:
enum class ExceptionType { kSystemError, kException, kString };

MockUDPClient(const net::IPAddress& serverAddr,
int maxPacketSize,
ExceptionType type)
: UDPClient(serverAddr, maxPacketSize)
, _type(type)
{
}

private:
void emitBatch(const thrift::Batch& batch) override
{
switch (_type) {
case ExceptionType::kSystemError:
throw std::system_error();
case ExceptionType::kException:
throw std::exception();
default:
assert(_type == ExceptionType::kString);
throw "error";
}
}

ExceptionType _type;
};

class MockUDPTransport : public UDPTransport {
public:
MockUDPTransport(const net::IPAddress& ip,
int maxPacketSize,
MockUDPClient::ExceptionType type)
: UDPTransport(ip, maxPacketSize)
{
setClient(std::unique_ptr<utils::UDPClient>(
new MockUDPClient(ip, maxPacketSize, type)));
}
};

} // anonymous namespace

TEST(UDPTransport, testManyMessages)
{
Expand All @@ -40,4 +84,25 @@ TEST(UDPTransport, testManyMessages)
}
}

TEST(UDPTransport, testExceptions)
{
const auto handle = testutils::TracerUtil::installGlobalTracer();
const auto tracer =
std::static_pointer_cast<const Tracer>(opentracing::Tracer::Global());

Span span(tracer);
span.SetOperationName("test");

const MockUDPClient::ExceptionType exceptionTypes[] = {
MockUDPClient::ExceptionType::kSystemError,
MockUDPClient::ExceptionType::kException,
MockUDPClient::ExceptionType::kString
};
for (auto type : exceptionTypes) {
MockUDPTransport sender(net::IPAddress(), 0, type);
sender.append(span);
ASSERT_THROW(sender.flush(), Transport::Exception);
}
}

} // namespace jaegertracing
4 changes: 2 additions & 2 deletions src/jaegertracing/utils/UDPClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Uber Technologies, Inc.
* Copyright (c) 2017-2018 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,7 @@ class UDPClient : public agent::thrift::AgentIf {
if (static_cast<int>(size) > _maxPacketSize) {
std::ostringstream oss;
oss << "Data does not fit within one UDP packet"
"; size "
", size "
<< size << ", max " << _maxPacketSize << ", spans "
<< batch.spans.size();
throw std::logic_error(oss.str());
Expand Down