diff --git a/Adapter.cc b/Adapter.cc index 9b8ec34..f4a776d 100644 --- a/Adapter.cc +++ b/Adapter.cc @@ -11,6 +11,8 @@ using namespace std; +thread_local pEp::Adapter::Session pEp::Adapter::session; + namespace pEp { void throw_status(PEP_STATUS status) { @@ -87,25 +89,26 @@ namespace pEp { return _sync_thread.get_id() == this_thread::get_id(); } - PEP_SESSION session(session_action action) + PEP_SESSION Session::operator()(session_action action) { std::lock_guard lock(m); bool in_sync = on_sync_thread(); - thread_local static PEP_SESSION _session = nullptr; PEP_STATUS status = PEP_STATUS_OK; switch (action) { case release: - if (_session) { - ::release(_session); + if (_session.get()) _session = nullptr; - } break; case init: - if (!_session) - status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); + if (!_session.get()) { + PEP_SESSION session_; + status = ::init(&session_, _messageToSend, _inject_sync_event, _ensure_passphrase); + throw_status(status); + _session = SessionPtr{session_, ::release}; + } break; default: @@ -113,7 +116,7 @@ namespace pEp { } throw_status(status); - return _session; + return _session.get(); } void shutdown() diff --git a/Adapter.hh b/Adapter.hh index d71fb33..31eca44 100644 --- a/Adapter.hh +++ b/Adapter.hh @@ -7,6 +7,8 @@ #include #include #include +#include + #include namespace pEp { @@ -45,7 +47,16 @@ namespace pEp { init, release }; - PEP_SESSION session(session_action action = init); + + class Session { + using SessionPtr = std::unique_ptr<_pEpSession, std::function>; + SessionPtr _session = nullptr; + + public: + PEP_SESSION operator()(session_action action = init); + }; + + extern thread_local Session session; // injects a NULL event into sync_event_queue to denote sync thread to shutdown, // and joins & removes the sync thread diff --git a/test/test_ensure_passphrase.cc b/test/test_ensure_passphrase.cc index b096c1f..ef7146d 100644 --- a/test/test_ensure_passphrase.cc +++ b/test/test_ensure_passphrase.cc @@ -51,7 +51,7 @@ int main(int argc, char **argv) ::free_identity(bob); ::free_identity(erwin); - session(Adapter::release); + // session(Adapter::release); return 0; } diff --git a/test/test_leave_device_group.cc b/test/test_leave_device_group.cc index 756cc7d..368ead9 100644 --- a/test/test_leave_device_group.cc +++ b/test/test_leave_device_group.cc @@ -1,4 +1,5 @@ #include +#include #include #include "framework.hh" @@ -11,19 +12,44 @@ using namespace pEp; using namespace pEp::Adapter; using namespace std; +vector expected_msg = { + "synchronizeGroupKeys", + "groupKeysUpdate", + "initUnledGroupKeyReset", + "beacon", + "beacon" + }; + +vector<::sync_handshake_signal> expected_notification = { + SYNC_NOTIFY_IN_GROUP, + SYNC_NOTIFY_START, + SYNC_NOTIFY_SOLE, + SYNC_NOTIFY_START, + SYNC_NOTIFY_STOP + }; + PEP_STATUS test_messageToSend(::message *_msg) { + static auto actual = expected_msg.begin(); + Test::Message msg = Test::make_message(_msg); - cerr << Test::make_pEp_msg(msg); + string text = Test::make_pEp_msg(msg); + cerr << "expecting: " << *actual << endl; + cerr << text; + assert(text.find(*actual++) != string::npos); return PEP_STATUS_OK; } PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_handshake_signal signal) { + static auto actual = expected_notification.begin(); + Test::Identity me = Test::make_identity(_me); Test::Identity partner = Test::make_identity(_partner); - + cerr << "expecting: " << *actual << endl; + cerr << "notifyHandshake: " << signal << endl; + assert(signal == *actual++); return PEP_STATUS_OK; } @@ -77,13 +103,16 @@ int main(int argc, char **argv) // wait for sync shutdown and release first session Test::join_sync_thread(); + assert(!is_sync_running()); // switch off and on again CallbackDispatcher::start_sync(); sleep(2); + assert(is_sync_running()); CallbackDispatcher::stop_sync(); Test::join_sync_thread(); + assert(!is_sync_running()); session(Adapter::release);