def io(self): connection = Connection("thread://io") """ mdsconnect """ self.assertEqual(connection.get('_a=1').tolist(), 1) self.assertEqual(connection.get('_a').tolist(), 1) self.assertEqual(connection.getObject('1:3:1').__class__, Range) g = GetMany(connection) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3)
def threadsTcp(self): server, server_port = self._setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 7100 + self.index, True) svr = svr_log = None try: svr, svr_log = self._start_mdsip(server, server_port, 'tcp') try: if svr is not None: time.sleep(1) def requests(c, idx): args = [Float32(i / 10 + idx) for i in range(10)] for i in range(10): self.assertEqual( c.get("[$,$,$,$,$,$,$,$,$,$]", *args).tolist(), args) c = Connection(server) """ mdsconnect """ self.assertEqual(c.get('_a=1').tolist(), 1) self.assertEqual(c.get('_a').tolist(), 1) self.assertEqual(c.getObject('1:3:1').__class__, Range) g = GetMany(c) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3) threads = [ Thread(name="C%d" % i, target=requests, args=(c, i)) for i in range(10) ] for thread in threads: thread.start() for thread in threads: thread.join() finally: if svr and svr.poll() is None: svr.terminate() svr.wait() finally: if svr_log: svr_log.close()
def test(): from time import sleep hosts = '%s/mdsip.hosts' % self.root def testDispatchCommand(mdsip, command, stdout=None, stderr=None): self.assertEqual( tcl( 'dispatch/command/nowait/server=%s %s' % (mdsip, command), 1, 1, 1), (None, None)) def setup_mdsip(server_env, port_env, default_port, fix0): host = getenv(server_env, '') if len(host) > 0: return host, 0 port = int(getenv(port_env, default_port)) if port == 0: if fix0: port = default_port else: return None, 0 return 'localhost:%d' % (port, ), port def start_mdsip(server, port, logname, env=None): if port > 0: from subprocess import Popen, STDOUT logfile = '%s_%d.log' % (logname, self.index) log = open(logfile, 'w') try: params = ['mdsip', '-s', '-p', str(port), '-h', hosts] print(' '.join(params + ['&>', logfile])) mdsip = Popen(params, env=env, stdout=log, stderr=STDOUT) except: log.close() raise return mdsip, log if server: for envpair in self.envx.items(): testDispatchCommand(server, 'env %s=%s' % envpair) return None, None monitor, monitor_port = setup_mdsip('ACTION_MONITOR', 'MONITOR_PORT', 8700 + self.index, False) monitor_opt = "/monitor=%s" % monitor if monitor_port > 0 else "" server, server_port = setup_mdsip('ACTION_SERVER', 'ACTION_PORT', 8800 + self.index, True) shot = self.shot + 1 Tree('pytree', -1, 'ReadOnly').createPulse(shot) show_server = "Checking server: %s\n[^,]+, [^,]+, logging enabled, Inactive\n" % server pytree = Tree('pytree', shot) pytree.TESTDEVICE.ACTIONSERVER.no_write_shot = False pytree.TESTDEVICE.ACTIONSERVER.record = server """ using dispatcher """ mon, mon_log, svr, svr_log = (None, None, None, None) try: mon, mon_log = start_mdsip(monitor, monitor_port, 'monitor') svr, svr_log = start_mdsip(server, server_port, 'server', self.env) try: sleep(3) if mon: self.assertEqual(mon.poll(), None) if svr: self.assertEqual(svr.poll(), None) """ mdsconnect """ c = Connection(server) self.assertEqual(c.get('1').tolist(), 1) self.assertEqual(c.getObject('1:3:1').__class__, Range) if not sys.platform.startswith( 'win'): # Windows does not support timeout yet try: # currently the connection needs to be closed after a timeout Connection(server).get("wait(1)", timeout=100) self.fail( "Connection.get(wait(1)) should have timed out." ) except Exc.MDSplusException as e: self.assertEqual(e.__class__, Exc.TdiTIMEOUT) g = GetMany(c) g.append('a', '1') g.append('b', '$', 2) g.append('c', '$+$', 1, 2) g.execute() self.assertEqual(g.get('a'), 1) self.assertEqual(g.get('b'), 2) self.assertEqual(g.get('c'), 3) """ tcl dispatch """ self._doTCLTest('show server %s' % server, out=show_server, re=True) testDispatchCommand(server, 'set verify') testDispatchCommand(server, 'type test') self._doTCLTest('set tree pytree/shot=%d' % shot) self._doTCLTest('dispatch/build%s' % monitor_opt) self._doTCLTest('dispatch/phase%s INIT' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) self._doTCLTest('dispatch/phase%s PULSE' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) self._doTCLTest('dispatch/phase%s STORE' % monitor_opt) sleep(1) self._doTCLTest('show server %s' % server, out=show_server, re=True) """ tcl exceptions """ self._doExceptionTest( 'dispatch/command/server=%s ' % server, Exc.MdsdclIVVERB) """ tcl check if still alive """ if mon: self.assertEqual(mon.poll(), None) if svr: self.assertEqual(svr.poll(), None) finally: try: self._doTCLTest( 'dispatch/command/wait/server=%s close/all' % server) finally: if svr and svr.poll() is None: svr.terminate() svr.wait() if mon and mon.poll() is None: mon.terminate() mon.wait() finally: if svr_log: svr_log.close() if mon_log: mon_log.close() self._doTCLTest('close/all') pytree = Tree('pytree', shot, 'ReadOnly') self.assertTrue(pytree.TESTDEVICE.INIT1_DONE.record <= pytree.TESTDEVICE.INIT2_DONE.record)