Skip to content

Commit

Permalink
Implement new mocking facilities
Browse files Browse the repository at this point in the history
  • Loading branch information
LourensVeen committed Jan 14, 2024
1 parent ecc7e35 commit d851f02
Show file tree
Hide file tree
Showing 24 changed files with 1,707 additions and 1,299 deletions.
2 changes: 1 addition & 1 deletion libmuscle/cpp/build/libmuscle/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public_headers := libmuscle/data.hpp libmuscle/data.tpp libmuscle/instance.hpp
public_headers += libmuscle/libmuscle.hpp libmuscle/mcp/data_pack.hpp
public_headers += libmuscle/mcp/data_pack.tpp libmuscle/message.hpp
public_headers += libmuscle/ports_description.hpp libmuscle/util.hpp libmuscle/util.tpp
public_headers += libmuscle/version.h libmuscle/namespace.hpp
public_headers += libmuscle/version.h libmuscle/namespace.hpp libmuscle/test_support.hpp
installed_headers := $(public_headers:%=$(PREFIX)/include/%)

pkg_config_files := libmuscle.pc
Expand Down
51 changes: 51 additions & 0 deletions libmuscle/cpp/src/libmuscle/tests/fixtures.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <gtest/gtest.h>

// Note: using POSIX for filesystem calls
// Could be upgraded to std::filesystem when targeting C++17 or later
#include <cstdlib>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <ftw.h>


// callback for nftw() to delete all contents of a folder
int _nftw_rm_callback(
const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
if (tflag == FTW_DP) {
std::cerr << "DEBUG: removing dir " << fpath << std::endl;
return rmdir(fpath);
}
if (tflag == FTW_F) {
std::cerr << "DEBUG: removing file " << fpath << std::endl;
return unlink(fpath);
}
std::cerr << "DEBUG: unknown file type " << fpath << std::endl;
return -1;
}

struct TempDirFixture {
TempDirFixture() {
char tmpname[] = "/tmp/muscle3_test.XXXXXX";
if (mkdtemp(tmpname) == nullptr) {
throw std::runtime_error(strerror(errno));
}
temp_dir_ = tmpname;
std::cerr << "DEBUG: using temp dir " << temp_dir_ << std::endl;
}

~TempDirFixture() {
// simulate rm -rf `temp_dir_` using a file-tree-walk
if (nftw(temp_dir_.c_str(), _nftw_rm_callback, 3, FTW_DEPTH) < 0) {
std::cerr << "ERROR: Could not remove temp dir at " << temp_dir_ << std::endl;
std::cerr << "ERROR: " << strerror(errno) << std::endl;
}
}

std::string temp_dir_;
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@

namespace libmuscle { namespace _MUSCLE_IMPL_NS { namespace mcp {

class MockTcpTransportServer : public TransportServer {
class MockTcpTransportServer
: public MockClass<MockTcpTransportServer>
, public TransportServer
{
public:
MockTcpTransportServer(RequestHandler & handler);
MockTcpTransportServer(ReturnValue) {
NAME_MOCK_MEM_FUN(MockTcpTransportServer, constructor);
NAME_MOCK_MEM_FUN(MockTcpTransportServer, get_location_mock);
NAME_MOCK_MEM_FUN(MockTcpTransportServer, close_mock);
}

~MockTcpTransportServer();
MockTcpTransportServer(RequestHandler & handler) {
init_from_return_value();
constructor(handler);
}

virtual std::string get_location() const;
MockFun<Void, Obj<RequestHandler &>> constructor;

virtual void close();
virtual std::string get_location() const override {
return get_location_mock();
}

// Mock control variables
static void reset();
mutable MockFun<Val<std::string>> get_location_mock;

static int num_constructed;
virtual void close() override {
return close_mock();
}

MockFun<Void> close_mock;
};

using TcpTransportServer = MockTcpTransportServer;
Expand Down
150 changes: 0 additions & 150 deletions libmuscle/cpp/src/libmuscle/tests/mocks/mock_communicator.cpp

This file was deleted.

Loading

0 comments on commit d851f02

Please sign in to comment.