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
|
/*
* Copyright information and license terms for this software can be
* found in the file LICENSE that is included with the distribution
*/
#include <pv/pvUnitTest.h>
#include <testMain.h>
#include <pva/client.h>
#include <pva/sharedstate.h>
#include <pv/current_function.h>
//#include <pv/pvAccess.h>
namespace pvd = epics::pvData;
namespace pva = epics::pvAccess;
namespace {
const pvd::StructureConstPtr type(pvd::getFieldCreate()->createFieldBuilder()
->add("value", pvd::pvInt)
->createStructure());
void testNoClient()
{
testDiag("==== %s ====", CURRENT_FUNCTION);
pvas::SharedPV::shared_pointer pv(pvas::SharedPV::buildReadOnly());
pvd::PVStructurePtr inst(pvd::getPVDataCreate()->createPVStructure(type));
pvd::BitSet changed;
testThrows(std::logic_error, pv->post(*inst, changed)); // not open()'d
pv->close(); // close while closed is a no-op
testThrows(std::logic_error, pv->build()); // not open()'d
}
void testGetMon()
{
testDiag("==== %s ====", CURRENT_FUNCTION);
std::tr1::shared_ptr<pvas::StaticProvider> prov(new pvas::StaticProvider("test"));
std::tr1::shared_ptr<pvas::SharedPV> pv(pvas::SharedPV::buildReadOnly());
prov->add("pv:name", pv);
pv->open(type);
pvd::PVStructurePtr inst(pvd::getPVDataCreate()->createPVStructure(type));
pvd::BitSet changed;
pvd::PVScalarPtr value(inst->getSubFieldT<pvd::PVScalar>("value"));
value->putFrom<pvd::uint32>(42);
changed.set(value->getFieldOffset());
pv->post(*inst, changed);
pvac::ClientProvider cli(prov->provider());
pvac::ClientChannel chan(cli.connect("pv:name"));
pvac::MonitorSync mon(chan.monitor());
{
pvd::PVStructure::const_shared_pointer R(chan.get());
testEqual(R->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::uint32>(), 42u);
}
testOk1(mon.test());
testEqual(mon.event.event, pvac::MonitorEvent::Data);
{
bool poll = mon.poll();
testOk1(poll);
if(poll) {
testEqual(mon.root->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::uint32>(), 42u);
} else {
testSkip(1, "No data");
}
}
testOk1(!mon.test());
testOk1(!mon.poll());
value->putFrom<pvd::uint32>(43);
pv->post(*inst, changed);
testOk1(mon.test());
{
bool poll = mon.poll();
testOk1(poll);
if(poll) {
testEqual(mon.root->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::uint32>(), 43u);
} else {
testSkip(1, "No data");
}
}
testOk1(!mon.test());
testOk1(!mon.poll());
}
void testPutRPCCancel()
{
testDiag("==== %s ====", CURRENT_FUNCTION);
std::tr1::shared_ptr<pvas::StaticProvider> prov(new pvas::StaticProvider("test"));
std::tr1::shared_ptr<pvas::SharedPV> pv(pvas::SharedPV::buildReadOnly());
prov->add("pv:name", pv);
pv->open(type);
pvac::ClientProvider cli(prov->provider());
pvac::ClientChannel chan(cli.connect("pv:name"));
testThrows(std::runtime_error, chan.put()
.set<pvd::uint32>("value", 44u)
.exec());
{
pvd::PVStructurePtr inst(pvd::getPVDataCreate()->createPVStructure(type));
testThrows(std::runtime_error, chan.rpc(1.0, inst))
}
}
struct TestPutRPCHandler : public pvas::SharedPV::Handler
{
virtual ~TestPutRPCHandler() {}
virtual void onPut(const pvas::SharedPV::shared_pointer& pv, pvas::Operation& op) OVERRIDE FINAL
{
pv->post(op.value(), op.changed());
op.complete();
}
virtual void onRPC(const pvas::SharedPV::shared_pointer& pv, pvas::Operation& op) OVERRIDE FINAL
{
pvd::PVStructurePtr reply(pvd::getPVDataCreate()->createPVStructure(type));
reply->getSubFieldT<pvd::PVScalar>("value")->putFrom<pvd::uint32>(100);
op.complete(*reply, pvd::BitSet());
}
};
void testPutRPC()
{
testDiag("==== %s ====", CURRENT_FUNCTION);
std::tr1::shared_ptr<pvas::StaticProvider> prov(new pvas::StaticProvider("test"));
std::tr1::shared_ptr<TestPutRPCHandler> handler(new TestPutRPCHandler);
std::tr1::shared_ptr<pvas::SharedPV> pv(pvas::SharedPV::build(handler));
prov->add("pv:name", pv);
pv->open(type);
pvac::ClientProvider cli(prov->provider());
pvac::ClientChannel chan(cli.connect("pv:name"));
{
pvd::PVStructure::const_shared_pointer R(chan.get());
testEqual(R->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::uint32>(), 0u);
}
chan.put()
.set<pvd::uint32>("value", 44u)
.exec();
{
pvd::PVStructure::const_shared_pointer R(chan.get());
testEqual(R->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::uint32>(), 44u);
}
pvd::PVStructurePtr arg(pvd::getPVDataCreate()->createPVStructure(type));
arg->getSubFieldT<pvd::PVScalar>("value")->putFrom<pvd::uint32>(50);
pvd::PVStructure::const_shared_pointer reply(chan.rpc(1.0, arg));
testEqual(reply->getSubFieldT<pvd::PVScalar>("value")->getAs<pvd::uint32>(), 100u);
}
} // namespace
MAIN(testsharedstate)
{
testPlan(19);
try {
testNoClient();
testGetMon();
testPutRPCCancel();
testPutRPC();
}catch(std::exception& e){
testAbort("Unexpected exception: %s", e.what());
}
return testDone();
}
|