static void checkArrI(const char *pv, long elen, epicsInt32 a, epicsInt32 b, epicsInt32 c, epicsInt32 d) { epicsInt32 buf[4]; epicsInt32 expect[4]; long nReq = NELEMENTS(buf), i; unsigned match; DBADDR addr; expect[0] = a; expect[1] = b; expect[2] = c; expect[3] = d; if (dbNameToAddr(pv, &addr)) testAbort("Unknown PV '%s'", pv); if (dbGet(&addr, DBR_LONG, buf, NULL, &nReq, NULL)) testAbort("Failed to get '%s'", pv); match = elen==nReq; for (i=0; i<nReq && i<elen; i++) { match &= buf[i]==expect[i]; } testOk(match, "dbGet(\"%s\") matches", pv); if (elen!=nReq) testDiag("lengths don't match %ld != %ld", elen, nReq); for (i=0; i<nReq && i<elen; i++) { if(buf[i]!=expect[i]) testDiag("[%ld] -> %d != %d", i, (int)expect[i], (int)buf[i]); } }
static void checkArrD(const char *pv, long elen, double a, double b, double c, double d) { double buf[4]; double expect[4]; long nReq = NELEMENTS(buf), i; unsigned match; DBADDR addr; expect[0] = a; expect[1] = b; expect[2] = c; expect[3] = d; if (dbNameToAddr(pv, &addr)) testAbort("Unknown PV '%s'", pv); if (dbGet(&addr, DBR_DOUBLE, buf, NULL, &nReq, NULL)) testAbort("Failed to get '%s'", pv); match = elen==nReq; for (i=0; i<nReq && i<elen; i++) { match &= fabs(buf[i]-expect[i])<0.01; } testOk(match, "dbGet(\"%s\") matches", pv); if (elen!=nReq) testDiag("lengths don't match %ld != %ld", elen, nReq); for (i=0; i<nReq && i<elen; i++) { if (fabs(buf[i]-expect[i])>=0.01) testDiag("[%ld] -> %f != %f", i, expect[i], buf[i]); } }
void epicsSpinPerformance () { static const unsigned N = 10000; unsigned i; epicsSpinId spin; epicsTimeStamp begin; epicsTimeStamp end; double delay; /* Initialize spinlock */ spin = epicsSpinCreate(); if (!spin) testAbort("epicsSpinCreate() returned NULL"); /* test a single lock pair */ epicsTimeGetCurrent(&begin); for ( i = 0; i < N; i++ ) { tenLockPairsSquared(spin); } epicsTimeGetCurrent(&end); delay = epicsTimeDiffInSeconds(&end, &begin); delay /= N * 100u; /* convert to delay per lock pair */ delay *= 1e6; /* convert to micro seconds */ testDiag("lock()*1/unlock()*1 takes %f microseconds", delay); epicsSpinDestroy(spin); }
int TestConnect(int argc, char* argv[]) { int randomPort; randomPort = 3389 + (random() % 200); /* Test connect to not existing server, * check if timeout is honored. */ if (testTimeout(randomPort)) return -1; /* Test connect to not existing server, * check if connection abort is working. */ if (testAbort(randomPort)) return -1; /* Test connect to existing server, * check if connection is working. */ if (testSuccess(randomPort)) return -1; return 0; }
static void testUDP(void) { caster_t caster; shSocket sender; osiSockAddr dest; union casterUDP buf; shSocketInit(&sender); sender.sd = shCreateSocket(AF_INET, SOCK_DGRAM, 0); if(sender.sd==INVALID_SOCKET) { testAbort("Failed to create socket"); return; } lock = epicsMutexMustCreate(); cycled[0] = epicsEventMustCreate(epicsEventEmpty); cycled[1] = epicsEventMustCreate(epicsEventEmpty); casterInit(&caster); caster.udpport = 0; /* test with random port */ caster.testhook = &testerhook; epicsThreadMustCreate("udptester", epicsThreadPriorityMedium, epicsThreadGetStackSize(epicsThreadStackSmall), &tester, &caster); epicsEventSignal(cycled[1]); /* wait for tester thread to setup socket */ epicsEventMustWait(cycled[0]); epicsMutexMustLock(lock); testOk1(caster.udpport!=0); testDiag("UDP test with port %d", caster.udpport); memset(&dest, 0, sizeof(dest)); dest.ia.sin_family = AF_INET; dest.ia.sin_addr.s_addr = htonl(INADDR_LOOPBACK); dest.ia.sin_port = htons(caster.udpport); epicsMutexUnlock(lock); /* allow tester thread to begin recv() */ epicsEventSignal(cycled[1]); testDiag("Test announcement directly from server"); memset(&buf, 0, sizeof(buf)); buf.m_msg.pid = htons(RECAST_MAGIC); buf.m_msg.serverIP = htonl(0xffffffff); buf.m_msg.serverPort = htons(0x1020); buf.m_msg.serverKey = htonl(0x12345678); testOk1(0==shSendTo(&sender, &buf.m_bytes, 0x10, 0, &dest)); /* wait for tester thread to completer recv() and end cycle */ epicsEventMustWait(cycled[0]); epicsMutexMustLock(lock); testOk1(cycles==1); testOk1(result==0); testOk1(caster.haveserv==1); testOk1(caster.nameserv.ia.sin_family==AF_INET); testOk1(caster.nameserv.ia.sin_addr.s_addr==htonl(INADDR_LOOPBACK)); testOk1(caster.nameserv.ia.sin_port==htons(0x1020)); testOk1(caster.servkey==0x12345678); epicsMutexUnlock(lock); testDiag("Test proxied announcement"); /* start next cycle */ epicsEventSignal(cycled[1]); /* wait for tester thread to setup socket */ epicsEventMustWait(cycled[0]); epicsMutexMustLock(lock); dest.ia.sin_port = htons(caster.udpport); epicsMutexUnlock(lock); buf.m_msg.serverIP = htonl(0x50607080); /* allow tester thread to begin recv() */ epicsEventSignal(cycled[1]); testOk1(0==shSendTo(&sender, &buf.m_bytes, 0x10, 0, &dest)); /* wait for tester thread to completer recv() and end cycle */ epicsEventMustWait(cycled[0]); epicsMutexMustLock(lock); testOk1(cycles==2); testOk1(result==0); testOk1(caster.haveserv==1); testOk1(caster.nameserv.ia.sin_family==AF_INET); testOk1(caster.nameserv.ia.sin_addr.s_addr==htonl(0x50607080)); testOk1(caster.nameserv.ia.sin_port==htons(0x1020)); epicsMutexUnlock(lock); /* begin shutdown cycle */ epicsEventSignal(cycled[1]); epicsEventMustWait(cycled[0]); epicsEventSignal(cycled[1]); casterShutdown(&caster); epicsEventDestroy(cycled[0]); epicsEventDestroy(cycled[1]); epicsMutexDestroy(lock); }
/* * Tests the log prefix code * The prefix is only applied to log messages as they go out to the socket, * so we need to create a server listening on a port, accept connections etc. * This code is a reduced version of the code in iocLogServer. */ static void testLogPrefix(void) { struct sockaddr_in serverAddr; int status; struct timeval timeout; struct sockaddr_in actualServerAddr; osiSocklen_t actualServerAddrSize; char portstring[16]; testDiag("Testing iocLogPrefix"); timeout.tv_sec = 5; /* in seconds */ timeout.tv_usec = 0; memset((void*)prefixmsgbuffer, 0, sizeof prefixmsgbuffer); /* Clear "errlog: <n> messages were discarded" status */ errlogPrintfNoConsole("."); errlogFlush(); sock = epicsSocketCreate(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { testAbort("epicsSocketCreate failed."); } /* We listen on a an available port. */ memset((void *)&serverAddr, 0, sizeof serverAddr); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(0); status = bind (sock, (struct sockaddr *)&serverAddr, sizeof (serverAddr) ); if (status < 0) { testAbort("bind failed; all ports in use?"); } status = listen(sock, 10); if (status < 0) { testAbort("listen failed!"); } /* Determine the port that the OS chose */ actualServerAddrSize = sizeof actualServerAddr; memset((void *)&actualServerAddr, 0, sizeof serverAddr); status = getsockname(sock, (struct sockaddr *) &actualServerAddr, &actualServerAddrSize); if (status < 0) { testAbort("Can't find port number!"); } sprintf(portstring, "%d", ntohs(actualServerAddr.sin_port)); testDiag("Listening on port %s", portstring); /* Set the EPICS environment variables for logging. */ epicsEnvSet ( "EPICS_IOC_LOG_INET", "localhost" ); epicsEnvSet ( "EPICS_IOC_LOG_PORT", portstring ); pfdctx = (void *) fdmgr_init(); if (status < 0) { testAbort("fdmgr_init failed!"); } status = fdmgr_add_callback(pfdctx, sock, fdi_read, acceptNewClient, &serverAddr); if (status < 0) { testAbort("fdmgr_add_callback failed!"); } testOk1(iocLogInit() == 0); fdmgr_pend_event(pfdctx, &timeout); testPrefixLogandCompare(prefixactualmsg[0]); iocLogPrefix(prefixstring); testPrefixLogandCompare(prefixactualmsg[1]); testPrefixLogandCompare(prefixactualmsg[2]); epicsSocketDestroy(sock); }