Example #1
0
class TestListenerContainer(unittest.TestCase):

    """Testing listener container"""

    def setUp(self):
        test_lock.acquire()
        self.ns = NameServer(max_age=timedelta(seconds=3))
        self.thr = Thread(target=self.ns.run)
        self.thr.start()

    def tearDown(self):
        self.ns.stop()
        self.thr.join()
        time.sleep(2)
        test_lock.release()

    def test_listener_container(self):
        """Test listener container"""
        pub = NoisyPublisher("test")
        pub.start()
        sub = ListenerContainer(topics=["/counter"])
        time.sleep(2)
        for counter in range(5):
            tested = False
            msg_out = Message("/counter", "info", str(counter))
            pub.send(str(msg_out))

            msg_in = sub.output_queue.get(True, 1)
            if msg_in is not None:
                self.assertEqual(str(msg_in), str(msg_out))
                tested = True
            self.assertTrue(tested)
        pub.stop()
        sub.stop()
Example #2
0
 def setUp(self):
     from posttroll.ns import NameServer
     test_lock.acquire()
     self.nameservers = ['localhost']
     self.ns = NameServer(max_age=timedelta(seconds=3),
                          multicast_enabled=False)
     self.thr = Thread(target=self.ns.run)
     self.thr.start()
Example #3
0
 def setUp(self):
     test_lock.acquire()
     self.nameservers = ['localhost']
     self.ns = NameServer(max_age=timedelta(seconds=3),
                          multicast_enabled=False)
     self.thr = Thread(target=self.ns.run)
     self.thr.start()
Example #4
0
class TestListenerContainer(unittest.TestCase):
    """Testing listener container"""
    def setUp(self):
        from posttroll.ns import NameServer
        test_lock.acquire()
        self.ns = NameServer(max_age=timedelta(seconds=3))
        self.thr = Thread(target=self.ns.run)
        self.thr.start()

    def tearDown(self):
        self.ns.stop()
        self.thr.join()
        time.sleep(2)
        test_lock.release()

    def test_listener_container(self):
        """Test listener container"""
        from posttroll.message import Message
        from posttroll.publisher import NoisyPublisher
        from posttroll.listener import ListenerContainer

        pub = NoisyPublisher("test")
        pub.start()
        sub = ListenerContainer(topics=["/counter"])
        time.sleep(2)
        for counter in range(5):
            tested = False
            msg_out = Message("/counter", "info", str(counter))
            pub.send(str(msg_out))

            msg_in = sub.output_queue.get(True, 1)
            if msg_in is not None:
                self.assertEqual(str(msg_in), str(msg_out))
                tested = True
            self.assertTrue(tested)
        pub.stop()
        sub.stop()
Example #5
0
 def setUp(self):
     from posttroll.ns import NameServer
     test_lock.acquire()
     self.ns = NameServer(max_age=timedelta(seconds=3))
     self.thr = Thread(target=self.ns.run)
     self.thr.start()
Example #6
0
class TestNS(unittest.TestCase):
    """Test the nameserver.
    """
    def setUp(self):
        from posttroll.ns import NameServer
        test_lock.acquire()
        self.ns = NameServer(max_age=timedelta(seconds=3))
        self.thr = Thread(target=self.ns.run)
        self.thr.start()

    def tearDown(self):
        self.ns.stop()
        self.thr.join()
        time.sleep(2)
        test_lock.release()

    def test_pub_addresses(self):
        """Test retrieving addresses."""
        from posttroll.ns import get_pub_addresses
        from posttroll.publisher import Publish

        with Publish(six.text_type("data_provider"),
                     0, ["this_data"],
                     broadcast_interval=0.1):
            time.sleep(.3)
            res = get_pub_addresses(["this_data"], timeout=.5)
            self.assertEqual(len(res), 1)
            expected = {
                u'status': True,
                u'service': [u'data_provider', u'this_data'],
                u'name': u'address'
            }
            for key, val in expected.items():
                self.assertEqual(res[0][key], val)
            self.assertTrue("receive_time" in res[0])
            self.assertTrue("URI" in res[0])
            res = get_pub_addresses([six.text_type("data_provider")])
            self.assertEqual(len(res), 1)
            expected = {
                u'status': True,
                u'service': [u'data_provider', u'this_data'],
                u'name': u'address'
            }
            for key, val in expected.items():
                self.assertEqual(res[0][key], val)
            self.assertTrue("receive_time" in res[0])
            self.assertTrue("URI" in res[0])

    def test_pub_sub_ctx(self):
        """Test publish and subscribe.
        """
        from posttroll.message import Message
        from posttroll.publisher import Publish
        from posttroll.subscriber import Subscribe

        with Publish("data_provider", 0, ["this_data"]) as pub:
            with Subscribe("this_data", "counter") as sub:
                for counter in range(5):
                    message = Message("/counter", "info", str(counter))
                    pub.send(str(message))
                    time.sleep(1)
                    msg = six.next(sub.recv(2))
                    if msg is not None:
                        self.assertEqual(str(msg), str(message))
                    tested = True
                sub.close()
        self.assertTrue(tested)

    def test_pub_sub_add_rm(self):
        """Test adding and removing publishers."""
        from posttroll.publisher import Publish
        from posttroll.subscriber import Subscribe

        time.sleep(4)
        with Subscribe("this_data", "counter", True) as sub:
            self.assertEqual(len(sub.sub_addr), 0)
            with Publish("data_provider", 0, ["this_data"]):
                time.sleep(4)
                six.next(sub.recv(2))
                self.assertEqual(len(sub.sub_addr), 1)
            time.sleep(3)
            for msg in sub.recv(2):
                if msg is None:
                    break
            time.sleep(3)
            self.assertEqual(len(sub.sub_addr), 0)
            with Publish("data_provider_2", 0, ["another_data"]):
                time.sleep(4)
                six.next(sub.recv(2))
                self.assertEqual(len(sub.sub_addr), 0)
            sub.close()
Example #7
0
 def setUp(self):
     self.ns = NameServer(max_age=timedelta(seconds=3))
     self.thr = Thread(target=self.ns.run)
     self.thr.start()
Example #8
0
class TestNS(unittest.TestCase):

    """Test the nameserver.
    """

    def setUp(self):
        self.ns = NameServer(max_age=timedelta(seconds=3))
        self.thr = Thread(target=self.ns.run)
        self.thr.start()

    def tearDown(self):
        self.ns.stop()
        self.thr.join()
        time.sleep(2)

    def test_pub_addresses(self):
        """Test retrieving addresses.
        """

        with Publish("data_provider", 0, ["this_data"]):
            time.sleep(3)
            res = get_pub_addresses(["this_data"])
            self.assertEquals(len(res), 1)
            expected = {u'status': True,
                        u'service': [u'data_provider', u'this_data'],
                        u'name': u'address'}
            for key, val in expected.items():
                self.assertEquals(res[0][key], val)
            self.assertTrue("receive_time" in res[0])
            self.assertTrue("URI" in res[0])
            res = get_pub_addresses(["data_provider"])
            self.assertEquals(len(res), 1)
            expected = {u'status': True,
                        u'service': [u'data_provider', u'this_data'],
                        u'name': u'address'}
            for key, val in expected.items():
                self.assertEquals(res[0][key], val)
            self.assertTrue("receive_time" in res[0])
            self.assertTrue("URI" in res[0])

    def test_pub_sub_ctx(self):
        """Test publish and subscribe.
        """

        with Publish("data_provider", 0, ["this_data"]) as pub:
            with Subscribe("this_data", "counter") as sub:
                for counter in range(5):
                    message = Message("/counter", "info", str(counter))
                    pub.send(str(message))
                    time.sleep(1)
                    msg = sub.recv(2).next()
                    if msg is not None:
                        self.assertEquals(str(msg), str(message))
                    tested = True
        self.assertTrue(tested)

    def test_pub_sub_add_rm(self):
        """Test adding and removing publishers.
        """

        with Subscribe("this_data", "counter", True) as sub:
            time.sleep(11)
            self.assertEquals(len(sub.sub_addr), 0)
            with Publish("data_provider", 0, ["this_data"]):
                time.sleep(4)
                sub.recv(2).next()
                self.assertEquals(len(sub.sub_addr), 1)
            time.sleep(3)
            for msg in sub.recv(2):
                if msg is None:
                    break
            time.sleep(3)
            self.assertEquals(len(sub.sub_addr), 0)
 def setUp(self):
     # Start a nameserver
     self.ns_ = NameServer(max_age=dt.timedelta(seconds=3))
     self.thr = Thread(target=self.ns_.run)
     self.thr.start()
class TestWorldCompositeDaemon(unittest.TestCase):

    adef = ADEF

    tslot = dt.datetime(2016, 10, 12, 12, 0)
    # Images from individual satellites
    sat_fnames = [
        os.path.join(THIS_DIR, "data", fname) for fname in [
            "20161012_1200_GOES-15_EPSG4326_wv.png",
            "20161012_1200_GOES-13_EPSG4326_wv.png",
            "20161012_1200_Meteosat-10_EPSG4326_wv.png",
            "20161012_1200_Meteosat-8_EPSG4326_wv.png",
            "20161012_1200_Himawari-8_EPSG4326_wv.png"
        ]
    ]

    # Image with all satellites merged without blending
    unblended = os.path.join(THIS_DIR, "data",
                             "20161012_1200_EPSG4326_wv_no_blend.png")
    # Empty image
    empty_image = os.path.join(THIS_DIR, "data", "empty.png")

    def setUp(self):
        # Start a nameserver
        self.ns_ = NameServer(max_age=dt.timedelta(seconds=3))
        self.thr = Thread(target=self.ns_.run)
        self.thr.start()

    def tearDown(self):
        # Stop nameserver
        self.ns_.stop()
        self.thr.join()

    def test_WorldCompositeDaemon(self):
        """Test WorldCompositeDaemon"""

        # Test incoming message handling and saving

        # Epoch: message sending time
        config = {
            "topics": ["/test"],
            "area_def": ADEF,
            "timeout_epoch": "message",
            "timeout": 45,
            "num_expected": 5,
            "out_pattern": os.path.join(THIS_DIR, "data", "test_out.png")
        }

        comp = gm.WorldCompositeDaemon(config)

        # There should be no slots
        self.assertEqual(len(comp.slots), 0)

        for i in range(len(self.sat_fnames)):
            msg = message.Message(
                "/test", "file", {
                    "uri": self.sat_fnames[i],
                    "nominal_time": self.tslot,
                    "productname": "wv"
                })
            epoch = msg.time
            comp._handle_message(msg)

            # Number of slots
            self.assertEqual(len(comp.slots), 1)

            # Number of composites
            self.assertEqual(len(comp.slots[self.tslot]), 1)

            # Number of files
            self.assertEqual(comp.slots[self.tslot]["wv"]["num"], i + 1)

            # Timeout
            diff = (comp.slots[self.tslot]["wv"]["timeout"] -
                    (epoch + dt.timedelta(minutes=config["timeout"])))
            self.assertAlmostEqual(diff.total_seconds(), 0.0, places=2)

            comp._check_timeouts_and_save()

            # Saving should not happen before all the images are received
            if i < 4:
                self.assertEqual(comp.slots[self.tslot]["wv"]["num"], i + 1)
            else:
                # After fifth image the composite should be saved and
                # all composites and slots removed
                self.assertEqual(len(comp.slots), 0)
                self.assertTrue(os.path.exists(config["out_pattern"]))
                # Remove the file
                os.remove(config["out_pattern"])

        comp.stop()

        # Epoch: file nominal time
        config = {
            "topics": ["/test"],
            "area_def": ADEF,
            "timeout_epoch": "nominal_time",
            "timeout": 45,
            "num_expected": 5,
            "out_pattern": os.path.join(THIS_DIR, "data", "test_out.png")
        }

        comp = gm.WorldCompositeDaemon(config)

        for i in range(len(self.sat_fnames)):
            msg = message.Message(
                "/test", "file", {
                    "uri": self.sat_fnames[i],
                    "nominal_time": self.tslot,
                    "productname": "wv"
                })
            epoch = self.tslot
            comp._handle_message(msg)

            # Number of slots
            self.assertEqual(len(comp.slots), 1)

            # Number of files should be one every time
            self.assertEqual(comp.slots[self.tslot]["wv"]["num"], 1)

            # Timeout
            self.assertEqual(comp.slots[self.tslot]["wv"]["timeout"],
                             (epoch + dt.timedelta(minutes=config["timeout"])))

            # Output file should be present after the first run
            if i > 0:
                self.assertTrue(os.path.exists(config["out_pattern"]))

            comp._check_timeouts_and_save()

            # There shouldn't be any slots now
            self.assertEqual(len(comp.slots), 0)

        # Remove the file
        os.remove(config["out_pattern"])

        # Stop compositor daemon
        comp.stop()
Example #11
0
 def setUp(self):
     self.ns = NameServer(max_age=timedelta(seconds=3))
     self.thr = Thread(target=self.ns.run)
     self.thr.start()
Example #12
0
class TestNS(unittest.TestCase):
    """Test the nameserver.
    """
    def setUp(self):
        self.ns = NameServer(max_age=timedelta(seconds=3))
        self.thr = Thread(target=self.ns.run)
        self.thr.start()

    def tearDown(self):
        self.ns.stop()
        self.thr.join()
        time.sleep(2)

    def test_pub_addresses(self):
        """Test retrieving addresses.
        """

        with Publish("data_provider", 0, ["this_data"]):
            time.sleep(3)
            res = get_pub_addresses(["this_data"])
            self.assertEquals(len(res), 1)
            expected = {
                u'status': True,
                u'service': [u'data_provider', u'this_data'],
                u'name': u'address'
            }
            for key, val in expected.items():
                self.assertEquals(res[0][key], val)
            self.assertTrue("receive_time" in res[0])
            self.assertTrue("URI" in res[0])
            res = get_pub_addresses(["data_provider"])
            self.assertEquals(len(res), 1)
            expected = {
                u'status': True,
                u'service': [u'data_provider', u'this_data'],
                u'name': u'address'
            }
            for key, val in expected.items():
                self.assertEquals(res[0][key], val)
            self.assertTrue("receive_time" in res[0])
            self.assertTrue("URI" in res[0])

    def test_pub_sub_ctx(self):
        """Test publish and subscribe.
        """

        with Publish("data_provider", 0, ["this_data"]) as pub:
            with Subscribe("this_data", "counter") as sub:
                for counter in range(5):
                    message = Message("/counter", "info", str(counter))
                    pub.send(str(message))
                    time.sleep(1)
                    msg = sub.recv(2).next()
                    if msg is not None:
                        self.assertEquals(str(msg), str(message))
                    tested = True
        self.assertTrue(tested)

    def test_pub_sub_add_rm(self):
        """Test adding and removing publishers.
        """

        with Subscribe("this_data", "counter", True) as sub:
            time.sleep(11)
            self.assertEquals(len(sub.sub_addr), 0)
            with Publish("data_provider", 0, ["this_data"]):
                time.sleep(4)
                sub.recv(2).next()
                self.assertEquals(len(sub.sub_addr), 1)
            time.sleep(3)
            for msg in sub.recv(2):
                if msg is None:
                    break
            time.sleep(3)
            self.assertEquals(len(sub.sub_addr), 0)