auto call release() on platforms where this is supported. Release_2.1.0 Release_2.1.0-RC25
authorVolker Birk <vb@pep-project.org>
Thu, 13 Aug 2020 23:47:30 +0200
changeset 355875a418fdffc
parent 354 58d9c9485459
child 356 f6113f798fc4
auto call release() on platforms where this is supported.
test messages and notifications.
Adapter.cc
Adapter.hh
test/test_ensure_passphrase.cc
test/test_leave_device_group.cc
     1.1 --- a/Adapter.cc	Thu Aug 13 14:49:28 2020 +0200
     1.2 +++ b/Adapter.cc	Thu Aug 13 23:47:30 2020 +0200
     1.3 @@ -11,6 +11,8 @@
     1.4  
     1.5  using namespace std;
     1.6  
     1.7 +thread_local pEp::Adapter::Session pEp::Adapter::session;
     1.8 +
     1.9  namespace pEp {
    1.10      void throw_status(PEP_STATUS status)
    1.11      {
    1.12 @@ -87,25 +89,26 @@
    1.13              return _sync_thread.get_id() == this_thread::get_id();
    1.14          }
    1.15  
    1.16 -        PEP_SESSION session(session_action action)
    1.17 +        PEP_SESSION Session::operator()(session_action action)
    1.18          {
    1.19              std::lock_guard<mutex> lock(m);
    1.20              bool in_sync = on_sync_thread();
    1.21  
    1.22 -            thread_local static PEP_SESSION _session = nullptr;
    1.23              PEP_STATUS status = PEP_STATUS_OK;
    1.24  
    1.25              switch (action) {
    1.26                  case release:
    1.27 -                    if (_session) {
    1.28 -                        ::release(_session);
    1.29 +                    if (_session.get())
    1.30                          _session = nullptr;
    1.31 -                    }
    1.32                      break;
    1.33  
    1.34                  case init:
    1.35 -                    if (!_session)
    1.36 -                        status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase);
    1.37 +                    if (!_session.get()) {
    1.38 +                        PEP_SESSION session_;
    1.39 +                        status = ::init(&session_, _messageToSend, _inject_sync_event, _ensure_passphrase);
    1.40 +                        throw_status(status);
    1.41 +                        _session = SessionPtr{session_, ::release};
    1.42 +                    }
    1.43                      break;
    1.44  
    1.45                  default:
    1.46 @@ -113,7 +116,7 @@
    1.47              }
    1.48  
    1.49              throw_status(status);
    1.50 -            return _session;
    1.51 +            return _session.get();
    1.52          }
    1.53  
    1.54          void shutdown()
     2.1 --- a/Adapter.hh	Thu Aug 13 14:49:28 2020 +0200
     2.2 +++ b/Adapter.hh	Thu Aug 13 23:47:30 2020 +0200
     2.3 @@ -7,6 +7,8 @@
     2.4  #include <string>
     2.5  #include <thread>
     2.6  #include <stdexcept>
     2.7 +#include <memory>
     2.8 +
     2.9  #include <pEp/sync_api.h>
    2.10  
    2.11  namespace pEp {
    2.12 @@ -45,7 +47,16 @@
    2.13              init,
    2.14              release
    2.15          };
    2.16 -        PEP_SESSION session(session_action action = init);
    2.17 +
    2.18 +        class Session {
    2.19 +            using SessionPtr = std::unique_ptr<_pEpSession, std::function<void(PEP_SESSION)>>;
    2.20 +            SessionPtr _session = nullptr;
    2.21 +
    2.22 +        public:
    2.23 +            PEP_SESSION operator()(session_action action = init);
    2.24 +        };
    2.25 +
    2.26 +        extern thread_local Session session;
    2.27  
    2.28          // injects a NULL event into sync_event_queue to denote sync thread to shutdown,
    2.29          // and joins & removes the sync thread
     3.1 --- a/test/test_ensure_passphrase.cc	Thu Aug 13 14:49:28 2020 +0200
     3.2 +++ b/test/test_ensure_passphrase.cc	Thu Aug 13 23:47:30 2020 +0200
     3.3 @@ -51,7 +51,7 @@
     3.4      ::free_identity(bob);
     3.5      ::free_identity(erwin);
     3.6  
     3.7 -    session(Adapter::release);
     3.8 +    // session(Adapter::release);
     3.9  
    3.10      return 0;
    3.11  }
     4.1 --- a/test/test_leave_device_group.cc	Thu Aug 13 14:49:28 2020 +0200
     4.2 +++ b/test/test_leave_device_group.cc	Thu Aug 13 23:47:30 2020 +0200
     4.3 @@ -1,4 +1,5 @@
     4.4  #include <iostream>
     4.5 +#include <vector>
     4.6  #include <unistd.h>
     4.7  
     4.8  #include "framework.hh"
     4.9 @@ -11,19 +12,44 @@
    4.10  using namespace pEp::Adapter;
    4.11  using namespace std;
    4.12  
    4.13 +vector<string> expected_msg = {
    4.14 +        "synchronizeGroupKeys",
    4.15 +        "groupKeysUpdate",
    4.16 +        "initUnledGroupKeyReset",
    4.17 +        "beacon",
    4.18 +        "beacon"
    4.19 +    };
    4.20 +
    4.21 +vector<::sync_handshake_signal> expected_notification = {
    4.22 +        SYNC_NOTIFY_IN_GROUP,
    4.23 +        SYNC_NOTIFY_START,
    4.24 +        SYNC_NOTIFY_SOLE,
    4.25 +        SYNC_NOTIFY_START,
    4.26 +        SYNC_NOTIFY_STOP
    4.27 +    };
    4.28 +
    4.29  PEP_STATUS test_messageToSend(::message *_msg)
    4.30  {
    4.31 +    static auto actual = expected_msg.begin();
    4.32 +
    4.33      Test::Message msg = Test::make_message(_msg);
    4.34 -    cerr << Test::make_pEp_msg(msg);
    4.35 +    string text = Test::make_pEp_msg(msg);
    4.36 +    cerr << "expecting: " << *actual << endl;
    4.37 +    cerr << text;
    4.38 +    assert(text.find(*actual++) != string::npos);
    4.39      return PEP_STATUS_OK;   
    4.40  }
    4.41  
    4.42  
    4.43  PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_handshake_signal signal)
    4.44  {
    4.45 +    static auto actual = expected_notification.begin();
    4.46 +
    4.47      Test::Identity me = Test::make_identity(_me);
    4.48      Test::Identity partner = Test::make_identity(_partner);
    4.49 -
    4.50 +    cerr << "expecting: " << *actual << endl;
    4.51 +    cerr << "notifyHandshake: " << signal << endl;
    4.52 +    assert(signal == *actual++);
    4.53      return PEP_STATUS_OK;   
    4.54  }
    4.55  
    4.56 @@ -77,13 +103,16 @@
    4.57      // wait for sync shutdown and release first session
    4.58  
    4.59      Test::join_sync_thread();
    4.60 +    assert(!is_sync_running());
    4.61  
    4.62      // switch off and on again
    4.63  
    4.64      CallbackDispatcher::start_sync();
    4.65      sleep(2);
    4.66 +    assert(is_sync_running());
    4.67      CallbackDispatcher::stop_sync();
    4.68      Test::join_sync_thread();
    4.69 +    assert(!is_sync_running());
    4.70  
    4.71      session(Adapter::release);
    4.72