Browse Source

auto call release() on platforms where this is supported.

test messages and notifications.
pull/1/head Release_2.1.0
Volker Birk 2 years ago
parent
commit
aa9a3c7e77
4 changed files with 55 additions and 12 deletions
  1. +11
    -8
      Adapter.cc
  2. +12
    -1
      Adapter.hh
  3. +1
    -1
      test/test_ensure_passphrase.cc
  4. +31
    -2
      test/test_leave_device_group.cc

+ 11
- 8
Adapter.cc View File

@ -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<mutex> 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()


+ 12
- 1
Adapter.hh View File

@ -7,6 +7,8 @@
#include <string>
#include <thread>
#include <stdexcept>
#include <memory>
#include <pEp/sync_api.h>
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<void(PEP_SESSION)>>;
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


+ 1
- 1
test/test_ensure_passphrase.cc View File

@ -51,7 +51,7 @@ int main(int argc, char **argv)
::free_identity(bob);
::free_identity(erwin);
session(Adapter::release);
// session(Adapter::release);
return 0;
}


+ 31
- 2
test/test_leave_device_group.cc View File

@ -1,4 +1,5 @@
#include <iostream>
#include <vector>
#include <unistd.h>
#include "framework.hh"
@ -11,19 +12,44 @@ using namespace pEp;
using namespace pEp::Adapter;
using namespace std;
vector<string> 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);


Loading…
Cancel
Save