Esempio n. 1
0
class TestFaceInterestMethods(ut.TestCase):
    def setUp(self):
        self.face = Face("aleph.ndn.ucla.edu")

    def tearDown(self):
        self.face.shutdown()

    def run_express_name_test(self, interestName, timeout=12):
        # returns the dataCallback and timeoutCallback mock objects so we can test timeout behavior
        # as well as a bool for if we timed out without timeoutCallback being called
        name = Name(interestName)
        dataCallback = Mock()
        timeoutCallback = Mock()
        self.face.expressInterest(name, dataCallback, timeoutCallback)

        def waitForCallbacks():
            while 1:
                self.face.processEvents()
                gevent.sleep()
                if (dataCallback.call_count > 0
                        or timeoutCallback.call_count > 0):
                    break

        task = gevent.spawn(waitForCallbacks)
        task.join(timeout=10)

        return dataCallback, timeoutCallback

    def test_any_interest(self):
        uri = "/"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)
        self.assertTrue(timeoutCallback.call_count == 0,
                        'Timeout on expressed interest')

        # check that the callback was correct
        self.assertEqual(
            dataCallback.call_count, 1,
            'Expected 1 onData callback, got ' + str(dataCallback.call_count))

        onDataArgs = dataCallback.call_args[
            0]  # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onDataArgs[0]
        self.assertTrue(callbackInterest.getName().equals(Name(uri)),
                        'Interest returned on callback had different name')

    def test_specific_interest(self):
        uri = "/ndn/edu/ucla/remap/ndn-js-test/howdy.txt/%FD%052%A1%DF%5E%A4"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)
        self.assertTrue(timeoutCallback.call_count == 0,
                        'Unexpected timeout on expressed interest')

        # check that the callback was correct
        self.assertEqual(
            dataCallback.call_count, 1,
            'Expected 1 onData callback, got ' + str(dataCallback.call_count))

        onDataArgs = dataCallback.call_args[
            0]  # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onDataArgs[0]
        self.assertTrue(callbackInterest.getName().equals(Name(uri)),
                        'Interest returned on callback had different name')

    def test_timeout(self):
        uri = "/test/timeout"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)

        # we're expecting a timeout callback, and only 1
        self.assertTrue(dataCallback.call_count == 0,
                        'Data callback called for invalid interest')

        self.assertTrue(
            timeoutCallback.call_count == 1,
            'Expected 1 timeout call, got ' + str(timeoutCallback.call_count))

        #check that the interest was returned correctly
        onTimeoutArgs = timeoutCallback.call_args[
            0]  # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onTimeoutArgs[0]
        self.assertTrue(callbackInterest.getName().equals(Name(uri)),
                        'Interest returned on callback had different name')

    def test_remove_pending(self):
        name = Name("/ndn/edu/ucla/remap/")
        dataCallback = Mock()
        timeoutCallback = Mock()

        interestID = self.face.expressInterest(name, dataCallback,
                                               timeoutCallback)

        def removeInterestAndWait():
            self.face.removePendingInterest(interestID)
            while 1:
                self.face.processEvents()
                gevent.sleep()
                currentTime = time.clock()
                if (dataCallback.call_count > 0
                        or timeoutCallback.call_count > 0):
                    break

        task = gevent.spawn(removeInterestAndWait)
        task.join(timeout=10)

        self.assertEqual(
            dataCallback.call_count, 0,
            'Should not have called data callback after interest was removed')
        self.assertEqual(
            timeoutCallback.call_count, 0,
            'Should not have called timeout callback after interest was removed'
        )
Esempio n. 2
0
class TestFaceInterestMethods(ut.TestCase):
    def setUp(self):
        self.face = Face("aleph.ndn.ucla.edu")

    def tearDown(self):
        self.face.shutdown()

    def run_express_name_test(self, interestName, timeout=12):
        # returns the dataCallback and timeoutCallback mock objects so we can test timeout behavior
        # as well as a bool for if we timed out without timeoutCallback being called
        name = Name(interestName)
        dataCallback = Mock()
        timeoutCallback = Mock()
        self.face.expressInterest(name, dataCallback, timeoutCallback)
        
        def waitForCallbacks():
            while 1:
                self.face.processEvents()
                gevent.sleep()
                if (dataCallback.call_count > 0 or timeoutCallback.call_count > 0):
                    break
        
        task = gevent.spawn(waitForCallbacks)
        task.join(timeout=10)

        return dataCallback, timeoutCallback



    def test_any_interest(self):
        uri = "/"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)
        self.assertTrue(timeoutCallback.call_count == 0 , 'Timeout on expressed interest')
        
        # check that the callback was correct
        self.assertEqual(dataCallback.call_count, 1, 'Expected 1 onData callback, got '+str(dataCallback.call_count))

        onDataArgs = dataCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onDataArgs[0]
        self.assertTrue(callbackInterest.getName().equals(Name(uri)), 'Interest returned on callback had different name')
        
    # TODO: Replace this with a test that connects to a Face on localhost
    #def test_specific_interest(self):
    #    uri = "/ndn/edu/ucla/remap/ndn-js-test/howdy.txt/%FD%052%A1%DF%5E%A4"
    #    (dataCallback, timeoutCallback) = self.run_express_name_test(uri)
    #    self.assertTrue(timeoutCallback.call_count == 0, 'Unexpected timeout on expressed interest')
    #    
    #    # check that the callback was correct
    #    self.assertEqual(dataCallback.call_count, 1, 'Expected 1 onData callback, got '+str(dataCallback.call_count))

    #    onDataArgs = dataCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

    #    #just check that the interest was returned correctly?
    #    callbackInterest = onDataArgs[0]
    #    self.assertTrue(callbackInterest.getName().equals(Name(uri)), 'Interest returned on callback had different name')

    def test_timeout(self):
        uri = "/test/timeout"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)

        # we're expecting a timeout callback, and only 1
        self.assertTrue(dataCallback.call_count == 0, 'Data callback called for invalid interest')

        self.assertTrue(timeoutCallback.call_count == 1, 'Expected 1 timeout call, got ' + str(timeoutCallback.call_count))

        #check that the interest was returned correctly
        onTimeoutArgs = timeoutCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onTimeoutArgs[0]
        self.assertTrue(callbackInterest.getName().equals(Name(uri)), 'Interest returned on callback had different name')

    def test_remove_pending(self):
        name = Name("/ndn/edu/ucla/remap/")
        dataCallback = Mock()
        timeoutCallback = Mock()

        interestID = self.face.expressInterest(name, dataCallback, timeoutCallback)

        def removeInterestAndWait():
            self.face.removePendingInterest(interestID)
            while 1:
                self.face.processEvents()
                gevent.sleep()
                currentTime = time.clock()
                if (dataCallback.call_count > 0 or timeoutCallback.call_count > 0):
                    break

        task = gevent.spawn(removeInterestAndWait)
        task.join(timeout=10)

        self.assertEqual(dataCallback.call_count, 0, 'Should not have called data callback after interest was removed')
        self.assertEqual(timeoutCallback.call_count, 0, 'Should not have called timeout callback after interest was removed')
Esempio n. 3
0
class TestFaceInterestMethods(ut.TestCase):
    def setUp(self):
        self.face = Face("localhost")

    def tearDown(self):
        self.face.shutdown()

    def run_express_name_test(self, interestName, timeout=12):
        # returns the dataCallback and timeoutCallback mock objects so we can test timeout behavior
        # as well as a bool for if we timed out without timeoutCallback being called
        name = Name(interestName)
        dataCallback = Mock()
        timeoutCallback = Mock()
        self.face.expressInterest(name, dataCallback, timeoutCallback)

        while True:
            self.face.processEvents()
            time.sleep(0.01)
            if (dataCallback.call_count > 0 or timeoutCallback.call_count > 0):
                break

        return dataCallback, timeoutCallback

    def test_any_interest(self):
        uri = "/"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)
        self.assertTrue(timeoutCallback.call_count == 0 , 'Timeout on expressed interest')

        # check that the callback was correct
        self.assertEqual(dataCallback.call_count, 1, 'Expected 1 onData callback, got '+str(dataCallback.call_count))

        onDataArgs = dataCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onDataArgs[0]
        self.assertTrue(callbackInterest.getName() == (Name(uri)), 'Interest returned on callback had different name')

    # TODO: Replace this with a test that connects to a Face on localhost
    #def test_specific_interest(self):
    #    uri = "/ndn/edu/ucla/remap/ndn-js-test/howdy.txt/%FD%052%A1%DF%5E%A4"
    #    (dataCallback, timeoutCallback) = self.run_express_name_test(uri)
    #    self.assertTrue(timeoutCallback.call_count == 0, 'Unexpected timeout on expressed interest')
    #
    #    # check that the callback was correct
    #    self.assertEqual(dataCallback.call_count, 1, 'Expected 1 onData callback, got '+str(dataCallback.call_count))

    #    onDataArgs = dataCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

    #    #just check that the interest was returned correctly?
    #    callbackInterest = onDataArgs[0]
    #    self.assertTrue(callbackInterest.getName() == Name(uri), 'Interest returned on callback had different name')

    def test_timeout(self):
        uri = "/test/timeout"
        (dataCallback, timeoutCallback) = self.run_express_name_test(uri)

        # we're expecting a timeout callback, and only 1
        self.assertTrue(dataCallback.call_count == 0, 'Data callback called for invalid interest')

        self.assertTrue(timeoutCallback.call_count == 1, 'Expected 1 timeout call, got ' + str(timeoutCallback.call_count))

        #check that the interest was returned correctly
        onTimeoutArgs = timeoutCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onTimeoutArgs[0]
        self.assertTrue(callbackInterest.getName() == (Name(uri)), 'Interest returned on callback had different name')

    def test_remove_pending(self):
        name = Name("/ndn/edu/ucla/remap/")
        dataCallback = Mock()
        timeoutCallback = Mock()

        interestID = self.face.expressInterest(name, dataCallback, timeoutCallback)

        self.face.removePendingInterest(interestID)

        timeout = 10000
        startTime = getNowMilliseconds()
        while True:
            self.face.processEvents()
            time.sleep(0.01)
            if getNowMilliseconds() - startTime >= timeout:
                break
            if (dataCallback.call_count > 0 or timeoutCallback.call_count > 0):
                break

        self.assertEqual(dataCallback.call_count, 0, 'Should not have called data callback after interest was removed')
        self.assertEqual(timeoutCallback.call_count, 0, 'Should not have called timeout callback after interest was removed')

    def test_max_ndn_packet_size(self):
        # Construct an interest whose encoding is one byte larger than getMaxNdnPacketSize.
        targetSize = Face.getMaxNdnPacketSize() + 1
        # Start with an interest which is almost the right size.
        interest = Interest()
        interest.getName().append(bytearray(targetSize))
        initialSize = interest.wireEncode().size()
        # Now replace the component with the desired size which trims off the extra encoding.
        interest.setName(
          (Name().append(bytearray(targetSize - (initialSize - targetSize)))))
        interestSize = interest.wireEncode().size()
        self.assertEqual(targetSize, interestSize,
          "Wrong interest size for MaxNdnPacketSize")

        with self.assertRaises(RuntimeError):
            # If no error is raised, then expressInterest didn't throw an
            # exception when the interest size exceeds getMaxNdnPacketSize()
            self.face.expressInterest(interest, Mock(), Mock())
Esempio n. 4
0
class TestFaceInterestMethods(ut.TestCase):
    def setUp(self):
        self.face = Face("localhost")

    def tearDown(self):
        self.face.shutdown()

    def run_express_name_test(self, interestName, useOnNack = False):
        # returns the dataCallback and timeoutCallback mock objects so we can test timeout behavior
        # as well as a bool for if we timed out without timeoutCallback being called
        name = Name(interestName)
        dataCallback = Mock()
        timeoutCallback = Mock()
        onNackCallback = Mock()

        if useOnNack:
            self.face.expressInterest(
              name, dataCallback, timeoutCallback, onNackCallback)
        else:
            self.face.expressInterest(name, dataCallback, timeoutCallback)

        while True:
            self.face.processEvents()
            time.sleep(0.01)
            if (dataCallback.call_count > 0 or timeoutCallback.call_count > 0 or
                onNackCallback.call_count > 0):
                break

        return dataCallback, timeoutCallback, onNackCallback

    def test_any_interest(self):
        uri = "/"
        (dataCallback, timeoutCallback, onNackCallback) = self.run_express_name_test(uri)
        self.assertTrue(timeoutCallback.call_count == 0 , 'Timeout on expressed interest')

        # check that the callback was correct
        self.assertEqual(dataCallback.call_count, 1, 'Expected 1 onData callback, got '+str(dataCallback.call_count))

        onDataArgs = dataCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onDataArgs[0]
        self.assertTrue(callbackInterest.getName() == (Name(uri)), 'Interest returned on callback had different name')

    # TODO: Replace this with a test that connects to a Face on localhost
    #def test_specific_interest(self):
    #    uri = "/ndn/edu/ucla/remap/ndn-js-test/howdy.txt/%FD%052%A1%DF%5E%A4"
    #    (dataCallback, timeoutCallback, onNackCallback) = self.run_express_name_test(uri)
    #    self.assertTrue(timeoutCallback.call_count == 0, 'Unexpected timeout on expressed interest')
    #
    #    # check that the callback was correct
    #    self.assertEqual(dataCallback.call_count, 1, 'Expected 1 onData callback, got '+str(dataCallback.call_count))

    #    onDataArgs = dataCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

    #    #just check that the interest was returned correctly?
    #    callbackInterest = onDataArgs[0]
    #    self.assertTrue(callbackInterest.getName() == Name(uri), 'Interest returned on callback had different name')

    def test_timeout(self):
        uri = "/test/timeout"
        (dataCallback, timeoutCallback, onNackCallback) = self.run_express_name_test(uri)

        # we're expecting a timeout callback, and only 1
        self.assertTrue(dataCallback.call_count == 0, 'Data callback called for invalid interest')

        self.assertTrue(timeoutCallback.call_count == 1, 'Expected 1 timeout call, got ' + str(timeoutCallback.call_count))

        #check that the interest was returned correctly
        onTimeoutArgs = timeoutCallback.call_args[0] # the args are returned as ([ordered arguments], [keyword arguments])

        #just check that the interest was returned correctly?
        callbackInterest = onTimeoutArgs[0]
        self.assertTrue(callbackInterest.getName() == (Name(uri)), 'Interest returned on callback had different name')

    def test_remove_pending(self):
        name = Name("/ndn/edu/ucla/remap/")
        dataCallback = Mock()
        timeoutCallback = Mock()

        interestID = self.face.expressInterest(name, dataCallback, timeoutCallback)

        self.face.removePendingInterest(interestID)

        timeout = 10000
        startTime = getNowMilliseconds()
        while True:
            if getNowMilliseconds() - startTime >= timeout:
                break
            self.face.processEvents()
            if (dataCallback.call_count > 0 or timeoutCallback.call_count > 0):
                break
            time.sleep(0.01)

        self.assertEqual(dataCallback.call_count, 0, 'Should not have called data callback after interest was removed')
        self.assertEqual(timeoutCallback.call_count, 0, 'Should not have called timeout callback after interest was removed')

    def test_max_ndn_packet_size(self):
        # Construct an interest whose encoding is one byte larger than getMaxNdnPacketSize.
        targetSize = Face.getMaxNdnPacketSize() + 1
        # Start with an interest which is almost the right size.
        interest = Interest()
        interest.getName().append(bytearray(targetSize))
        initialSize = interest.wireEncode().size()
        # Now replace the component with the desired size which trims off the extra encoding.
        interest.setName(
          (Name().append(bytearray(targetSize - (initialSize - targetSize)))))
        interestSize = interest.wireEncode().size()
        self.assertEqual(targetSize, interestSize,
          "Wrong interest size for MaxNdnPacketSize")

        with self.assertRaises(RuntimeError):
            # If no error is raised, then expressInterest didn't throw an
            # exception when the interest size exceeds getMaxNdnPacketSize()
            self.face.expressInterest(interest, Mock(), Mock())

    def test_network_nack(self):
        uri = "/noroute" + str(getNowMilliseconds())
        (dataCallback, timeoutCallback, onNackCallback) = self.run_express_name_test(
          uri, True)

        # We're expecting a network Nack callback, and only 1.
        self.assertEqual(dataCallback.call_count, 0,
          "Data callback called for unroutable interest")
        self.assertEqual(timeoutCallback.call_count, 0,
          "Timeout callback called for unroutable interest")
        self.assertEqual(onNackCallback.call_count, 1,
          "Expected 1 network Nack call")

        # The args are returned as ([ordered arguments], [keyword arguments])
        onNetworkNackArgs = onNackCallback.call_args[0]

        callbackNetworkNack = onNetworkNackArgs[1]
        self.assertEqual(callbackNetworkNack.getReason(), NetworkNack.Reason.NO_ROUTE,
          "Network Nack has unexpected reason")