Added more low-level tests and implemented enums

This commit is contained in:
mpilman 2019-01-31 14:45:42 -08:00
parent 0a3676111e
commit 7affe73774
4 changed files with 152 additions and 6 deletions

View File

@ -234,6 +234,27 @@ TEST_CASE("/fdbclient/MonitorLeader/parseConnectionString/basic") {
}
TEST_CASE("/flow/FlatBuffers/LeaderInfo") {
{
LeaderInfo in;
LeaderInfo out;
in.forward = g_random->coinflip();
in.changeID = g_random->randomUniqueID();
{
std::string rndString(g_random->randomInt(10, 400), 'x');
for (auto& c : rndString) {
c = g_random->randomAlphaNumeric();
}
in.serializedInfo = rndString;
}
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(in.forward == out.forward);
ASSERT(in.changeID == out.changeID);
ASSERT(in.serializedInfo == out.serializedInfo);
}
LeaderInfo leaderInfo;
leaderInfo.forward = g_random->coinflip();
leaderInfo.changeID = g_random->randomUniqueID();
@ -247,11 +268,9 @@ TEST_CASE("/flow/FlatBuffers/LeaderInfo") {
ErrorOr<Optional<LeaderInfo>> objIn{ Optional<LeaderInfo>{leaderInfo} };
ErrorOr<Optional<LeaderInfo>> objOut;
Standalone<StringRef> copy;
{
ObjectWriter writer;
writer.serialize(objIn);
copy = writer.toStringRef();
}
ObjectWriter writer;
writer.serialize(objIn);
copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(objOut);

View File

@ -126,3 +126,26 @@ private:
uint8_t* data = nullptr;
int size = 0;
};
// this special case is needed - the code expects
// Standalone<T> and T to be equivalent for serialization
namespace detail {
template <class T>
struct LoadSaveHelper<Standalone<T>> {
template <class Context>
void load(Standalone<T>& member, const uint8_t* current, Context& context) {
helper.load(member.contents(), current, context);
context.addArena(member.arena());
}
template <class Writer>
RelativeOffset save(const Standalone<T>& member, Writer& writer, const VTableSet* vtables) {
return helper.save(member.contents(), writer, vtables);
}
private:
LoadSaveHelper<T> helper;
};
} // namespace detail

View File

@ -19,6 +19,7 @@
*/
#include "flow/flow.h"
#include "flow/UnitTest.h"
#include <stdarg.h>
#include <cinttypes>
@ -214,3 +215,78 @@ bool validationIsEnabled() {
void enableBuggify( bool enabled ) {
buggifyActivated = enabled;
}
TEST_CASE("/flow/FlatBuffers/ErrorOr") {
{
ErrorOr<int> in(worker_removed());
ErrorOr<int> out;
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(out.isError());
ASSERT(out.getError().code() == in.getError().code());
}
{
ErrorOr<uint32_t> in(g_random->randomUInt32());
ErrorOr<uint32_t> out;
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(!out.isError());
ASSERT(out.get() == in.get());
}
return Void();
}
TEST_CASE("/flow/FlatBuffers/Optional") {
{
Optional<int> in;
Optional<int> out;
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(!out.present());
}
{
Optional<uint32_t> in(g_random->randomUInt32());
Optional<uint32_t> out;
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(out.present());
ASSERT(out.get() == in.get());
}
return Void();
}
TEST_CASE("/flow/FlatBuffers/Standalone") {
{
Standalone<StringRef> in(std::string("foobar"));
StringRef out;
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(in == out);
}
{
StringRef in = LiteralStringRef("foobar");
Standalone<StringRef> out;
ObjectWriter writer;
writer.serialize(in);
Standalone<StringRef> copy = writer.toStringRef();
ArenaObjectReader reader(copy.arena(), copy);
reader.deserialize(out);
ASSERT(in == out);
}
return Void();
}

View File

@ -191,13 +191,41 @@ public:
bool isError() const { return error.code() != invalid_error_code; }
bool isError(int code) const { return error.code() == code; }
Error getError() const { ASSERT(isError()); return error; }
const Error& getError() const { ASSERT(isError()); return error; }
private:
typename std::aligned_storage< sizeof(T), __alignof(T) >::type value;
Error error;
};
template <class T>
struct union_like_traits<ErrorOr<T>> : std::true_type {
using Member = ErrorOr<T>;
using alternatives = pack<Error, T>;
static uint8_t index(const Member& variant) { return variant.present() ? 1 : 0; }
static bool empty(const Member& variant) { return false; }
template <int i>
static const index_t<i, alternatives>& get(const Member& m) {
if constexpr (i == 0) {
return m.getError();
} else {
static_assert(i == 1, "ErrorOr only has two members");
return m.get();
}
}
template <int i, class Alternative>
static const void assign(Member& m, const Alternative& a) {
if constexpr (i == 0) {
m = a;
} else {
static_assert(i == 1);
m = a;
}
}
};
template <class T>
struct Callback {
Callback<T> *prev, *next;