1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

fix: fixed deadlock in tests (wait for client to connect, instead on relying on the server)

This commit is contained in:
Falk Werner 2020-06-13 23:47:52 +02:00
parent ed39db2d8d
commit edcad5dc4c
3 changed files with 54 additions and 39 deletions

View File

@ -66,22 +66,21 @@ TEST(AdapterClient, Connect)
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1);
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1);
bool connected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
bool disconnected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
client.Connect();
while (!server.IsConnected())
{
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
client.Disconnect();
while (server.IsConnected())
{
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
}
TEST(AdapterClient, Authenticate)
@ -95,32 +94,32 @@ TEST(AdapterClient, Authenticate)
MockAdapterClientCallback callback;
EXPECT_CALL(callback, Invoke(_, _, _)).Times(AnyNumber());
bool connected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
bool disconnected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_AUTHENTICATE_GET_CREDENTIALS, _)).Times(1)
.WillOnce(Invoke(GetCredentials));
bool called = false;
bool authenticated = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_AUTHENTICATED, nullptr)).Times(1)
.WillOnce(Invoke([&called] (wf_client *, int, void *) mutable {
called = true;
}));
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { authenticated = true; }));
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
client.Connect();
while (!server.IsConnected())
{
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
client.Authenticate();
while (!called) {
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return authenticated; }));
client.Disconnect();
while (server.IsConnected())
{
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
}
TEST(AdapterClient, AuthenticateFailedWithoutConnect)
@ -140,9 +139,7 @@ TEST(AdapterClient, AuthenticateFailedWithoutConnect)
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), "");
client.Authenticate();
while (!called) {
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return called; }));
}
TEST(AdapterClient, AuthenticationFailed)
@ -156,8 +153,18 @@ TEST(AdapterClient, AuthenticationFailed)
MockAdapterClientCallback callback;
EXPECT_CALL(callback, Invoke(_, _, _)).Times(AnyNumber());
bool connected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
bool disconnected = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_AUTHENTICATE_GET_CREDENTIALS, _)).Times(1)
.WillOnce(Invoke(GetCredentials));
bool called = false;
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_AUTHENTICATION_FAILED, nullptr)).Times(1)
.WillOnce(Invoke([&called] (wf_client *, int, void *) mutable {
@ -167,19 +174,11 @@ TEST(AdapterClient, AuthenticationFailed)
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
client.Connect();
while (!server.IsConnected())
{
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
client.Authenticate();
while (!called) {
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return called; }));
client.Disconnect();
while (server.IsConnected())
{
watcher.check();
}
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
}

View File

@ -1,5 +1,6 @@
#include "webfuse/utils/timeout_watcher.hpp"
#include <stdexcept>
#include <thread>
using std::chrono::milliseconds;
using std::chrono::duration_cast;
@ -41,4 +42,17 @@ void TimeoutWatcher::check()
}
}
bool TimeoutWatcher::waitUntil(std::function<bool()> predicate)
{
bool result = predicate();
while ((!result) && (!isTimeout()))
{
std::this_thread::yield();
result = predicate();
}
return result;
}
}

View File

@ -2,6 +2,7 @@
#define WF_TEST_TIMEOUT_WATCHER_HPP
#include <chrono>
#include <functional>
namespace webfuse_test
{
@ -15,6 +16,7 @@ public:
~TimeoutWatcher();
bool isTimeout();
void check();
bool waitUntil(std::function<bool()> predicate);
private:
std::chrono::milliseconds startedAt;
std::chrono::milliseconds timeout_;