88Tests the XBee (IEEE 802.15.4) implementation class for XBee API compliance
99"""
1010import unittest
11+ try :
12+ from unittest .mock import Mock
13+ except ImportError :
14+ from mock import Mock
1115from xbee .tornado import has_tornado
1216
1317if not has_tornado :
1721from xbee .tornado .ieee import XBee # noqa
1822from xbee .frame import APIFrame # noqa
1923from xbee .python2to3 import intToByte , stringToBytes # noqa
24+ from tornado import ioloop # noqa
2025from tornado .testing import AsyncTestCase , gen_test # noqa
2126from tornado .test .util import unittest # noqa
2227import sys # noqa
@@ -33,7 +38,10 @@ def setUp(self):
3338 Initialize XBee object
3439 """
3540 super (InitXBee , self ).setUp ()
36- self .xbee = XBee (None )
41+ self ._patch_io = ioloop .IOLoop .current ()
42+ self ._patch_io .add_handler = Mock ()
43+ serial_port = Serial ()
44+ self .xbee = XBee (serial_port , io_loop = self ._patch_io )
3745
3846
3947class TestBuildCommand (InitXBee ):
@@ -462,7 +470,7 @@ def test_send_at_command(self):
462470 """
463471
464472 serial_port = Serial ()
465- xbee = XBee (serial_port )
473+ xbee = XBee (serial_port , io_loop = self . _patch_io )
466474
467475 # Send an AT command
468476 xbee .send ('at' , frame_id = stringToBytes ('A' ),
@@ -480,7 +488,7 @@ def test_send_at_command_with_param(self):
480488 """
481489
482490 serial_port = Serial ()
483- xbee = XBee (serial_port )
491+ xbee = XBee (serial_port , io_loop = self . _patch_io )
484492
485493 # Send an AT command
486494 xbee .send (
@@ -508,7 +516,7 @@ def setUp(self):
508516 """
509517 super (TestSendShorthand , self ).setUp ()
510518 self .ser = Serial ()
511- self .xbee = XBee (self .ser )
519+ self .xbee = XBee (self .ser , io_loop = self . _patch_io )
512520
513521 def test_send_at_command (self ):
514522 """
@@ -552,7 +560,7 @@ def test_shorthand_disabled(self):
552560 When shorthand is disabled, any attempt at calling a
553561 non-existant attribute should raise AttributeError
554562 """
555- self .xbee = XBee (self .ser , shorthand = False )
563+ self .xbee = XBee (self .ser , shorthand = False , io_loop = self . _patch_io )
556564
557565 try :
558566 self .xbee .at
@@ -575,7 +583,7 @@ def test_read_at(self):
575583 """
576584 device = Serial ()
577585 device .set_read_data (b'\x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
578- xbee = XBee (device )
586+ xbee = XBee (device , io_loop = self . _patch_io )
579587
580588 xbee ._process_input (None , None )
581589 info = yield xbee .wait_read_frame ()
@@ -592,7 +600,7 @@ def test_read_at_params(self):
592600 """
593601 device = Serial ()
594602 device .set_read_data (b'\x7E \x00 \x08 \x88 DMY\x01 \x00 \x00 \x00 \x8c ' )
595- xbee = XBee (device )
603+ xbee = XBee (device , io_loop = self . _patch_io )
596604
597605 xbee ._process_input (None , None )
598606 info = yield xbee .wait_read_frame ()
@@ -620,7 +628,7 @@ def test_is_response_parsed_as_io(self):
620628
621629 device = Serial ()
622630 device .set_read_data (APIFrame (data = b'\x88 DIS\x00 ' + data ).output ())
623- xbee = XBee (device )
631+ xbee = XBee (device , io_loop = self . _patch_io )
624632
625633 xbee ._process_input (None , None )
626634 info = yield xbee .wait_read_frame ()
@@ -655,7 +663,7 @@ def test_is_remote_response_parsed_as_io(self):
655663 data = b'\x97 D\x00 \x13 \xa2 \x00 @oG\xe4 v\x1a IS\x00 ' + data ).output ()
656664 )
657665
658- xbee = XBee (device )
666+ xbee = XBee (device , io_loop = self . _patch_io )
659667
660668 xbee ._process_input (None , None )
661669 info = yield xbee .wait_read_frame ()
@@ -693,7 +701,7 @@ def test_read_io_data(self):
693701
694702 device = Serial ()
695703 device .set_read_data (b'\x7E \x00 \x0C ' + rx_io_resp + data + b'\xfd ' )
696- xbee = XBee (device )
704+ xbee = XBee (device , io_loop = self . _patch_io )
697705
698706 xbee ._process_input (None , None )
699707 info = yield xbee .wait_read_frame ()
@@ -737,7 +745,7 @@ def read(self, length=1):
737745 return super (BadReadDevice , self ).read ()
738746
739747 badDevice = BadReadDevice (1 , b'\x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
740- xbee = XBee (badDevice )
748+ xbee = XBee (badDevice , io_loop = self . _patch_io )
741749
742750 try :
743751 xbee ._process_input (None , None )
@@ -755,7 +763,7 @@ def test_read_at_params_in_escaped_mode(self):
755763 """
756764 device = Serial ()
757765 device .set_read_data (b'~\x00 \t \x88 DMY\x01 }^}]}1}3m' )
758- xbee = XBee (device , escaped = True )
766+ xbee = XBee (device , escaped = True , io_loop = self . _patch_io )
759767
760768 xbee ._process_input (None , None )
761769 info = yield xbee .wait_read_frame ()
@@ -773,7 +781,7 @@ def test_empty_frame_ignored(self):
773781 """
774782 device = Serial ()
775783 device .set_read_data (b'\x7E \x00 \x00 \xFF \x7E \x00 \x05 \x88 DMY\x01 \x8c ' )
776- xbee = XBee (device )
784+ xbee = XBee (device , io_loop = self . _patch_io )
777785
778786 xbee ._process_input (None , None )
779787 xbee ._process_input (None , None )
@@ -791,7 +799,7 @@ def test_read_rx_with_close_brace(self):
791799 """
792800 device = Serial ()
793801 device .set_read_data (APIFrame (b'\x81 \x01 \x02 \x55 \x00 {test=1}' ).output ())
794- xbee = XBee (device )
802+ xbee = XBee (device , io_loop = self . _patch_io )
795803
796804 xbee ._process_input (None , None )
797805 info = yield xbee .wait_read_frame ()
@@ -810,7 +818,7 @@ def test_read_rx_with_close_brace_escaped(self):
810818 device = Serial ()
811819 device .set_read_data (APIFrame (b'\x81 \x01 \x02 \x55 \x00 {test=1}' ,
812820 escaped = True ).output ())
813- xbee = XBee (device , escaped = True )
821+ xbee = XBee (device , escaped = True , io_loop = self . _patch_io )
814822
815823 xbee ._process_input (None , None )
816824 info = yield xbee .wait_read_frame ()
0 commit comments