test/src/engine_tests/SyncTests.cc
author Volker Birk <vb@pep.foundation>
Wed, 22 Aug 2018 20:01:29 +0200
branchsync
changeset 2873 ceaa29b309ef
parent 2871 4593a7b8b6a8
child 2874 45531079b073
permissions -rw-r--r--
sync thread for test
vb@2852
     1
// This file is under GNU General Public License 3.0
vb@2852
     2
// see LICENSE.txt
vb@2852
     3
vb@2852
     4
#include <stdlib.h>
vb@2852
     5
#include <string>
vb@2873
     6
#include <thread>
vb@2852
     7
vb@2852
     8
#include "pEpEngine.h"
vb@2852
     9
#include "sync_api.h"
vb@2871
    10
vb@2871
    11
#include "pEp_internal.h"
vb@2871
    12
#include "KeySync_fsm.h"
vb@2852
    13
vb@2852
    14
#include "EngineTestSessionSuite.h"
vb@2852
    15
#include "SyncTests.h"
vb@2852
    16
vb@2852
    17
#include "locked_queue.hh"
vb@2852
    18
vb@2852
    19
using namespace std;
vb@2852
    20
vb@2852
    21
class Sync_Adapter {
vb@2852
    22
public:
vb@2852
    23
    utility::locked_queue< Sync_event_t * > q;
vb@2852
    24
vb@2852
    25
    static PEP_STATUS notifyHandshake(
vb@2871
    26
            void *obj,
vb@2871
    27
            pEp_identity *me,
vb@2871
    28
            pEp_identity *partner,
vb@2871
    29
            sync_handshake_signal signal
vb@2871
    30
        )
vb@2852
    31
    {
vb@2852
    32
        return PEP_STATUS_OK;
vb@2852
    33
    }
vb@2852
    34
vb@2852
    35
    static int inject_sync_event(SYNC_EVENT ev, void *management)
vb@2852
    36
    {
vb@2853
    37
        auto adapter = static_cast< Sync_Adapter *>(management);
vb@2853
    38
        adapter->q.push_front(ev);
vb@2852
    39
        return 0;
vb@2852
    40
    }
vb@2864
    41
vb@2852
    42
    static Sync_event_t *retrieve_next_sync_event(void *management)
vb@2852
    43
    {
vb@2853
    44
        auto adapter = static_cast< Sync_Adapter *>(management);
vb@2873
    45
        while (adapter->q.empty()) {
vb@2873
    46
            sleep(1);
vb@2873
    47
        }
vb@2873
    48
        Sync_event_t *result = adapter->q.pop_front();
vb@2873
    49
        if (result)
vb@2873
    50
            cout << "retrieving sync event\n";
vb@2873
    51
        else
vb@2873
    52
            cout << "retrieving shutdown\n";
vb@2873
    53
        return result;
vb@2852
    54
    }
vb@2871
    55
vb@2871
    56
    static PEP_STATUS messageToSend(void *obj, struct _message *msg)
vb@2871
    57
    {
vb@2871
    58
        assert(msg);
vb@2873
    59
        cout << "messageToSend\n";
vb@2871
    60
        return PEP_STATUS_OK;
vb@2871
    61
    }
vb@2873
    62
vb@2873
    63
    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
vb@2873
    64
    {
vb@2873
    65
        cout << "sync_thread: startup\n";
vb@2873
    66
        do_sync_protocol(session, adapter);
vb@2873
    67
        cout << "sync_thread: shutdown\n";
vb@2873
    68
    }
vb@2852
    69
};
vb@2852
    70
vb@2852
    71
SyncTests::SyncTests(string suitename, string test_home_dir) :
vb@2852
    72
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
vb@2852
    73
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
vb@2852
    74
                                                                      static_cast<Func>(&SyncTests::check_sync)));
vb@2852
    75
}
vb@2852
    76
vb@2871
    77
void SyncTests::check_sync()
vb@2871
    78
{
vb@2852
    79
    Sync_Adapter adapter;
vb@2871
    80
    PEP_SESSION sync = NULL;
vb@2873
    81
    thread *sync_thread;
vb@2852
    82
vb@2871
    83
    PEP_STATUS status = init(&sync, Sync_Adapter::messageToSend);
vb@2871
    84
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2871
    85
vb@2871
    86
    status = register_sync_callbacks(
vb@2871
    87
            sync,
vb@2852
    88
            &adapter.q,
vb@2852
    89
            Sync_Adapter::notifyHandshake,
vb@2852
    90
            Sync_Adapter::inject_sync_event,
vb@2852
    91
            Sync_Adapter::retrieve_next_sync_event
vb@2852
    92
        );
vb@2871
    93
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2873
    94
    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
vb@2852
    95
vb@2873
    96
    cout << "creating thread for sync\n";
vb@2873
    97
    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
vb@2873
    98
 
vb@2873
    99
    cout << "sending shutdown to sync thread\n";
vb@2873
   100
    adapter.q.push_front(nullptr);
vb@2873
   101
    sync_thread->join();
vb@2871
   102
vb@2871
   103
    unregister_sync_callbacks(sync);
vb@2871
   104
    release(sync);
vb@2852
   105
}
vb@2852
   106