File size: 12,016 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
  Author(s):  Anton Deguet
  Created on: 2011-01-06

  (C) Copyright 2011-2026 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/

#include "mtsCollectorStateTest.h"

#include <cisstMultiTask/mtsManagerLocal.h>
#include <cisstMultiTask/mtsCollectorState.h>

#include "mtsTestComponents.h"


mtsCollectorStateTest::mtsCollectorStateTest()
{
}


void mtsCollectorStateTest::setUp(void)
{
}


void mtsCollectorStateTest::tearDown(void)
{
}

template <class _serverType>
void mtsCollectorStateTest::TestExecution(_serverType * server,
                                          double serverExecutionDelay)
{
    const std::string filename = "StateDataCollectionUnitTest";

    // execution result used by all functions
    mtsExecutionResult executionResult;

    // we assume both server and servers use the same type
    mtsManagerLocal * manager = mtsManagerLocal::GetInstance();
    manager->RemoveAllUserComponents();

    // add to manager and start all
    CPPUNIT_ASSERT(manager->AddComponent(server));

    mtsCollectorState * stateCollector = new mtsCollectorState("CollectorStateTest");
    stateCollector->SetOutput(filename, mtsCollectorBase::COLLECTOR_FILE_FORMAT_CSV);
    CPPUNIT_ASSERT(stateCollector);
    CPPUNIT_ASSERT(stateCollector->SetStateTable(server->GetName(),
                                                 server->InterfaceProvided1.StateTable->GetName()));
    CPPUNIT_ASSERT(stateCollector->AddSignal(""));
    CPPUNIT_ASSERT(manager->AddComponent(stateCollector));
    CPPUNIT_ASSERT(stateCollector->Connect());

    // add test device to control execution
    mtsCollectorStateTestDevice * stateCollectorTestDevice = new mtsCollectorStateTestDevice();
    std::string stateTableName = server->InterfaceProvided1.StateTable->GetName();
    std::string stateTableInterfaceName = "StateTable" + server->InterfaceProvided1.StateTable->GetName();
    CPPUNIT_ASSERT(stateCollectorTestDevice);
    CPPUNIT_ASSERT(manager->AddComponent(stateCollectorTestDevice));
    CPPUNIT_ASSERT(manager->Connect(stateCollectorTestDevice->GetName(), "TestComponent",
                                    server->GetName(), "p1"));
    CPPUNIT_ASSERT(manager->Connect(stateCollectorTestDevice->GetName(), "StateTable",
                                    server->GetName(), stateTableInterfaceName));
    CPPUNIT_ASSERT(manager->Connect(stateCollectorTestDevice->GetName(), "CollectorState",
                                    stateCollector->GetName(), "Control"));
    manager->CreateAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::READY, StateTransitionMaximumDelay));
    manager->StartAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::ACTIVE, StateTransitionMaximumDelay));

    // test that functions are connected
    CPPUNIT_ASSERT(stateCollectorTestDevice->TestComponent.StateTableAdvance.IsValid());
    CPPUNIT_ASSERT(stateCollectorTestDevice->CollectorState.StartCollection.IsValid());
    CPPUNIT_ASSERT(stateCollectorTestDevice->CollectorState.StopCollection.IsValid());

    // actual testing, preliminary conditions
    mtsStateTable::IndexRange range;
    CPPUNIT_ASSERT(!stateCollectorTestDevice->CollectionRunning);
    CPPUNIT_ASSERT_EQUAL(0u, stateCollectorTestDevice->BatchReadyEventCounter);
    CPPUNIT_ASSERT_EQUAL(0u, stateCollectorTestDevice->SamplesCollected);

    // try a start/stop without any state table advance
    executionResult = stateCollectorTestDevice->CollectorState.StartCollection.ExecuteBlocking(0.0);
    CPPUNIT_ASSERT(executionResult.IsOK());
    // commands are sent to the collector which then sends to the
    // server which owns the state table.  the later command being
    // queued we need to wait a bit
    osaSleep(serverExecutionDelay);
    // collection will not send the start event until the next state table advance
    CPPUNIT_ASSERT(!stateCollectorTestDevice->CollectionRunning);
    // advance state table, this will trigger the start event
    executionResult = stateCollectorTestDevice->TestComponent.StateTableAdvance.ExecuteBlocking();
    CPPUNIT_ASSERT(executionResult.IsOK());
    CPPUNIT_ASSERT(stateCollectorTestDevice->CollectionRunning);
    // to make sure, not collection should have been performed so far
    CPPUNIT_ASSERT_EQUAL(0u, stateCollectorTestDevice->BatchReadyEventCounter);
    CPPUNIT_ASSERT_EQUAL(0u, stateCollectorTestDevice->SamplesCollected);
    // now try to stop
    executionResult = stateCollectorTestDevice->CollectorState.StopCollection.ExecuteBlocking(0.0);
    CPPUNIT_ASSERT(executionResult.IsOK());
    // see previous comment re. osaSleep
    osaSleep(serverExecutionDelay);
    // collection will not send the stop event until the next state table advance
    executionResult = stateCollectorTestDevice->TestComponent.StateTableAdvance.ExecuteBlocking();
    CPPUNIT_ASSERT(executionResult.IsOK());
    CPPUNIT_ASSERT(!stateCollectorTestDevice->CollectionRunning);
    // since we had to do one advance, the collector should send a range event
    CPPUNIT_ASSERT_EQUAL(1u, stateCollectorTestDevice->BatchReadyEventCounter);
    CPPUNIT_ASSERT_EQUAL(1u, stateCollectorTestDevice->SamplesCollected);

#if 0

    // now collect 300 elements so we can test replay
    executionResult = stateCollectorTestDevice->CollectorState.StartCollection.ExecuteBlocking(0.0);
    CPPUNIT_ASSERT(executionResult.IsOK());
    // see previous comment re. osaSleep
    osaSleep(serverExecutionDelay);
    for (size_t index = 0;
         index < 300;
         index++) {
        executionResult = stateCollectorTestDevice->TestComponent.StateTableAdvance.ExecuteBlocking();
        CPPUNIT_ASSERT(executionResult.IsOK());
        CPPUNIT_ASSERT(stateCollectorTestDevice->CollectionRunning);
    }
    // stop collection
    executionResult = stateCollectorTestDevice->CollectorState.StopCollection.ExecuteBlocking(0.0);
    CPPUNIT_ASSERT(executionResult.IsOK());
    // see previous comment re. osaSleep
    osaSleep(serverExecutionDelay);
    // collection will not send the stop event until the next state table advance
    executionResult = stateCollectorTestDevice->TestComponent.StateTableAdvance.ExecuteBlocking();
    CPPUNIT_ASSERT(executionResult.IsOK());
    // since we had to do one advance, the collector should send a range event
    CPPUNIT_ASSERT_EQUAL(11u, stateCollectorTestDevice->BatchReadyEventCounter);
    CPPUNIT_ASSERT_EQUAL(300u, stateCollectorTestDevice->SamplesCollected);

    // create a new component of the same type for replay
    _serverType * replayComponent = new _serverType("replay-component");
    CPPUNIT_ASSERT(manager->AddComponent(replayComponent));
    // should fail since we are not in replay mode
    CPPUNIT_ASSERT(!replayComponent->SetReplayData(stateTableName, filename));
    CPPUNIT_ASSERT(replayComponent->SetReplayMode());
    // shouldn't find that "dummy" state table
    CPPUNIT_ASSERT(!replayComponent->SetReplayData("dummy", filename));
    std::string dataReplayFile = filename + ".csv";
    CPPUNIT_ASSERT(replayComponent->SetReplayData(stateTableName, dataReplayFile));

    // create and connect component to read from replay state table
    typedef typename _serverType::value_type value_type;
    mtsTestDevice1<value_type> * replayReader = new mtsTestDevice1<value_type>("replay-reader");
    CPPUNIT_ASSERT(manager->AddComponent(replayReader));
    CPPUNIT_ASSERT(manager->Connect(replayReader->GetName(), "r1",
                                    replayComponent->GetName(), "p1"));

    replayComponent->Start();
    replayReader->Start();
    replayComponent->Create();
    replayReader->Create();

    value_type data;
    for (size_t index = 0;
         index < 300;
         index++) {
        CPPUNIT_ASSERT(replayComponent->InterfaceProvided1.StateTable->ReplayAdvance());
        replayReader->InterfaceRequired1.FunctionStateTableRead(data);
        std::cerr << index << " -> " << data << std::endl;
    }

#endif

    // stop all and cleanup
    manager->KillAll();
    CPPUNIT_ASSERT(manager->WaitForStateAll(mtsComponentState::FINISHED, StateTransitionMaximumDelay));

    CPPUNIT_ASSERT(stateCollector->Disconnect());
    CPPUNIT_ASSERT(manager->Disconnect(stateCollectorTestDevice->GetName(), "TestComponent",
                                       server->GetName(), "p1"));
    CPPUNIT_ASSERT(manager->Disconnect(stateCollectorTestDevice->GetName(), "StateTable",
                                       server->GetName(), stateTableInterfaceName));
    CPPUNIT_ASSERT(manager->Disconnect(stateCollectorTestDevice->GetName(), "CollectorState",
                                       stateCollector->GetName(), "Control"));
    CPPUNIT_ASSERT(manager->RemoveComponent(server));
    CPPUNIT_ASSERT(manager->RemoveComponent(stateCollector));
    CPPUNIT_ASSERT(manager->RemoveComponent(stateCollectorTestDevice));
    delete stateCollector;
    delete stateCollectorTestDevice;
}


template <class _elementType>
void mtsCollectorStateTest::TestDevice(void)
{
    mtsTestDevice1<_elementType> * server = new mtsTestDevice1<_elementType>;
    TestExecution(server, 0.0 /* no execution delay for a device */);
    delete server;
}
void mtsCollectorStateTest::TestDevice_mtsInt(void) {
    mtsCollectorStateTest::TestDevice<mtsInt>();
}
void mtsCollectorStateTest::TestDevice_int(void) {
    mtsCollectorStateTest::TestDevice<int>();
}


template <class _elementType>
void mtsCollectorStateTest::TestPeriodic(void)
{
    mtsTestPeriodic1<_elementType> * server = new mtsTestPeriodic1<_elementType>;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(server, serverExecutionDelay);
    delete server;
}
void mtsCollectorStateTest::TestPeriodic_mtsInt(void) {
    mtsCollectorStateTest::TestPeriodic<mtsInt>();
}
void mtsCollectorStateTest::TestPeriodic_int(void) {
    mtsCollectorStateTest::TestPeriodic<int>();
}


template <class _elementType>
void mtsCollectorStateTest::TestContinuous(void)
{
    mtsTestContinuous1<_elementType> * server = new mtsTestContinuous1<_elementType>;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(server, serverExecutionDelay);
    delete server;
}
void mtsCollectorStateTest::TestContinuous_mtsInt(void) {
    mtsCollectorStateTest::TestContinuous<mtsInt>();
}
void mtsCollectorStateTest::TestContinuous_int(void) {
    mtsCollectorStateTest::TestContinuous<int>();
}


template <class _elementType>
void mtsCollectorStateTest::TestFromCallback(void)
{
    mtsTestFromCallback1<_elementType> * server = new mtsTestFromCallback1<_elementType>;
    mtsTestCallbackTrigger * serverTrigger = new mtsTestCallbackTrigger(server);
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(server, serverExecutionDelay);
    serverTrigger->Stop();
    delete serverTrigger;
    delete server;
}
void mtsCollectorStateTest::TestFromCallback_mtsInt(void) {
    mtsCollectorStateTest::TestFromCallback<mtsInt>();
}
void mtsCollectorStateTest::TestFromCallback_int(void) {
    mtsCollectorStateTest::TestFromCallback<int>();
}


template <class _elementType>
void mtsCollectorStateTest::TestFromSignal(void)
{
    mtsTestFromSignal1<_elementType> * server = new mtsTestFromSignal1<_elementType>;
    const double serverExecutionDelay = 0.1 * cmn_s;
    TestExecution(server, serverExecutionDelay);
    delete server;
}
void mtsCollectorStateTest::TestFromSignal_mtsInt(void) {
    mtsCollectorStateTest::TestFromSignal<mtsInt>();
}
void mtsCollectorStateTest::TestFromSignal_int(void) {
    mtsCollectorStateTest::TestFromSignal<int>();
}

CPPUNIT_TEST_SUITE_REGISTRATION(mtsCollectorStateTest);