Пример #1
0
def test_tutorial_4_forEach():
    print("CallbackList tutorial 4, forEach")
    # create a CallbackList
    callbackList = CallbackList()

    # Add some callbacks.
    callbackList.append(lambda: print("Got callback 1."))
    callbackList.append(lambda: print("Got callback 2."))
    callbackList.append(lambda: print("Got callback 3."))

    # Now call forEach to remove the second callback
    # The forEach callback prototype is func(handle, callback)
    index = 0

    def func(handle, callback):
        nonlocal index
        print("forEach(Handle, Callback), invoked ", index)
        if index == 1:
            callbackList.remove(handle)
            print("forEach(Handle, Callback), removed second callback")
        index += 1

    callbackList.forEach(func)

    # The forEach callback prototype can also be func(callback)
    callbackList.forEach(lambda callback: print("forEach(Callback), invoked"))

    # Invoke the callback list
    # The "Got callback 2" callback should not be triggered.
    callbackList()
Пример #2
0
def test_hasParams():
    dataList = []

    def cb1(s, i):
        dataList.append(s + str(i + 1))

    def cb2(s, i):
        dataList.append(s + str(i + 2))

    def cb3(s, i):
        dataList.append(s + str(i + 3))

    callbackList = CallbackList()
    assert callbackList.empty()

    h1 = callbackList.append(cb1)
    assert not callbackList.empty()
    dataList = []
    callbackList('hello', 5)
    assert dataList == ['hello6']

    h2 = callbackList.append(cb2)
    assert not callbackList.empty()
    dataList = []
    callbackList('hello', 5)
    assert dataList == ['hello6', 'hello7']

    h3 = callbackList.insert(cb3, h2)
    assert not callbackList.empty()
    dataList = []
    callbackList('hello', 5)
    assert dataList == ['hello6', 'hello8', 'hello7']
Пример #3
0
def test_nestedCallbacks_newCallbacksShouldNotBeTriggered():
    a = 0
    b = 0
    callbackList = CallbackList()

    def cb1():
        nonlocal b
        b += 1
        callbackList.append(cbIncB)
        h2 = callbackList.append(cb2)
        callbackList.append(cbIncB)
        callbackList.insert(cbIncB, h2)
        callbackList.prepend(cbIncB)

    def cb2():
        nonlocal b
        b += 1

    def cbIncB():
        nonlocal b
        b += 1
        callbackList.append(cbIncB)

    def cb0():
        nonlocal a
        a = 1
        h1 = callbackList.append(cb1)
        callbackList.prepend(cbIncB)
        callbackList.insert(cbIncB, h1)

    callbackList.append(cb0)

    assert a == 0
    assert b == 0

    callbackList()
    assert a == 1
    assert b == 0

    callbackList()
    assert a == 1
    assert b == 3  # there are 3 new top level callback

    b = 0
    callbackList()
    assert a == 1
    assert b > 3
Пример #4
0
def test_forEachIf():
    dataList = [0 for x in range(3)]

    def cb1():
        dataList[0] += 1

    def cb2():
        dataList[1] += 2

    def cb3():
        dataList[2] += 3

    callbackList = CallbackList()
    callbackList.append(cb1)
    callbackList.append(cb2)
    callbackList.append(cb3)

    assert dataList == [0, 0, 0]

    i = 0

    def func1(cb):
        nonlocal i
        cb()
        i += 1
        return i != 2

    result = callbackList.forEachIf(func1)

    assert not result
    assert dataList == [1, 2, 0]
Пример #5
0
def test_tutorial_3_remove():
    print("CallbackList tutorial 3, remove")
    # create a CallbackList
    callbackList = CallbackList()

    # Add some callbacks.
    callbackList.append(lambda: print("Got callback 1."))
    handle2 = callbackList.append(lambda: print("Got callback 2."))
    callbackList.append(lambda: print("Got callback 3."))

    callbackList.remove(handle2)

    # Invoke the callback list
    # The "Got callback 2" callback should not be triggered.
    callbackList()
Пример #6
0
def test_doubleRemove():
    threadCount = 64
    taskCountPerThread = 1024
    itemCount = threadCount * taskCountPerThread

    callbackList = CallbackList()
    dataList = [0 for x in range(itemCount)]
    taskList = []
    for i in range(itemCount):

        def cb(i=i):
            dataList[i] = i

        taskList.append(cb)
    random.shuffle(taskList)

    handleList = []
    for item in taskList:
        handleList.append(callbackList.append(item))

    threadList = []
    for i in range(threadCount):

        def cb(i=i):
            start = i
            end = i + 1
            if i > 0:
                start -= 1
            elif i < threadCount - 1:
                end += 1
            for k in range(start * taskCountPerThread,
                           end * taskCountPerThread):
                callbackList.remove(handleList[k])

        threadList.append(threading.Thread(target=cb))

    assert not callbackList.empty()
    for thread in threadList:
        thread.start()
    for thread in threadList:
        thread.join()

    assert callbackList.empty()
Пример #7
0
    def test(self):
        callbackList = CallbackList()

        handleList = []
        dataList = []
        compareList = []

        for i in range(self._callbackCount):
            handleList.append(None)
            dataList.append(0)
            compareList.append(i + 1)

        def createNormalCallback(index):
            def cb():
                dataList[index] = index + 1

            return cb

        def createRemovalCallback(index):
            def cb():
                dataList[index] = index + 1
                for i in self._indexesToBeRemoved:
                    callbackList.remove(handleList[i])

            return cb

        for i in range(self._callbackCount):
            if i == self._removerIndex:
                handleList[i] = callbackList.append(createRemovalCallback(i))
            else:
                handleList[i] = callbackList.append(createNormalCallback(i))

        callbackList()

        for i in self._indexesToBeRemoved:
            if i > self._removerIndex:
                compareList[i] = 0

        assert dataList == compareList
Пример #8
0
def test_remove():
    threadCount = 64
    taskCountPerThread = 1024
    itemCount = threadCount * taskCountPerThread

    callbackList = CallbackList()
    dataList = [0 for x in range(itemCount)]
    taskList = []
    for i in range(itemCount):

        def cb(i=i):
            dataList[i] = i

        taskList.append(cb)
    random.shuffle(taskList)

    handleList = []
    for item in taskList:
        handleList.append(callbackList.append(item))

    threadList = []
    for i in range(threadCount):

        def cb(i=i):
            for k in range(i * taskCountPerThread,
                           (i + 1) * taskCountPerThread):
                callbackList.remove(handleList[k])

        threadList.append(threading.Thread(target=cb))

    assert not callbackList.empty()
    for thread in threadList:
        thread.start()
    for thread in threadList:
        thread.join()

    assert callbackList.empty()
Пример #9
0
def test_insert():
    threadCount = 64
    taskCountPerThread = 1024
    itemCount = threadCount * taskCountPerThread

    callbackList = CallbackList()
    dataList = [0 for x in range(itemCount)]
    taskList = []
    for i in range(itemCount):

        def cb(i=i):
            dataList[i] = i

        cb.testData = i
        taskList.append(cb)
    random.shuffle(taskList)

    handleList = [None for x in range(itemCount)]
    threadList = []
    for i in range(threadCount):

        def cb(i=i):
            k = i * taskCountPerThread
            for k in range(k,
                           i * taskCountPerThread + taskCountPerThread // 2):
                handleList[k] = callbackList.append(taskList[k])
            offset = 0
            for k in range(
                    k + 1, i * taskCountPerThread + taskCountPerThread // 2 +
                    taskCountPerThread // 4):
                handleList[k] = callbackList.insert(taskList[k],
                                                    handleList[offset])
                offset += 1
            for k in range(k + 1, (i + 1) * taskCountPerThread):
                handleList[k] = callbackList.insert(taskList[k],
                                                    handleList[offset])
                offset += 1

        threadList.append(threading.Thread(target=cb))

    for thread in threadList:
        thread.start()
    for thread in threadList:
        thread.join()

    callbackList()
    dataList.sort()
    compareList = [x for x in range(itemCount)]
    assert dataList == compareList
Пример #10
0
def test_tutorial_2_callbackWithParameters():
    print("CallbackList tutorial 2, callback with parameters")
    # create a CallbackList
    callbackList = CallbackList()
    callbackList.append(lambda s, b: print("Got callback 1, s is %s b is %d" %
                                           (s, b)))

    def anotherCallback(s, b):
        print("Got callback 2, s is %s b is %d" % (s, b))

    callbackList.append(anotherCallback)
    # Invoke the callback list
    callbackList("Hello world", True)
Пример #11
0
def test_tutorial_1_basic():
    print("CallbackList tutorial 1, basic")
    # create a CallbackList
    callbackList = CallbackList()
    # Add a callback.
    # Lambda is not required, any function or callable object is fine
    callbackList.append(lambda: print("Got callback 1."))

    def anotherCallback():
        print("Got callback 2.")

    callbackList.append(anotherCallback)
    # Invoke the callback list
    callbackList()
Пример #12
0
def test_append():
    threadCount = 64
    taskCountPerThread = 1024
    itemCount = threadCount * taskCountPerThread

    callbackList = CallbackList()
    dataList = [0 for x in range(itemCount)]
    taskList = []
    for i in range(itemCount):

        def cb(i=i):
            dataList[i] = i

        taskList.append(cb)
    random.shuffle(taskList)

    threadList = []
    for i in range(threadCount):

        def cb(i=i):
            for k in range(i * taskCountPerThread,
                           (i + 1) * taskCountPerThread):
                callbackList.append(taskList[k])

        threadList.append(threading.Thread(target=cb))

    for thread in threadList:
        thread.start()
    for thread in threadList:
        thread.join()

    taskList = []
    callbackList()

    dataList.sort()
    compareList = [x for x in range(itemCount)]
    assert dataList == compareList
Пример #13
0
def test_noParams():
    dataList = []

    def cb1():
        dataList.append(1)

    def cb2():
        dataList.append(2)

    def cb3():
        dataList.append(3)

    callbackList = CallbackList()
    assert callbackList.empty()

    h1 = callbackList.append(cb1)
    assert not callbackList.empty()
    dataList = []
    callbackList()
    assert dataList == [1]

    callbackList._currentCounter = -2  # trigger _currentCounter overflow
    h2 = callbackList.append(cb2)
    assert not callbackList.empty()
    dataList = []
    callbackList()
    assert dataList == [1, 2]

    h3 = callbackList.insert(cb3, h2)
    assert not callbackList.empty()
    dataList = []
    callbackList()
    assert dataList == [1, 3, 2]

    callbackList.remove(h2)
    assert not callbackList.empty()
    dataList = []
    callbackList()
    assert dataList == [1, 3]

    h2 = callbackList.prepend(cb2)
    assert not callbackList.empty()
    dataList = []
    callbackList()
    assert dataList == [2, 1, 3]

    callbackList.remove(h1)
    callbackList.remove(h2)
    callbackList.remove(h3)
    assert callbackList.empty()
    dataList = []
    callbackList()
    assert dataList == []
Пример #14
0
def test_forEach():
    def cb1():
        return 1

    def cb2():
        return 2

    def cb3():
        return 3

    callbackList = CallbackList(policy.singleThreadPolicy)
    callbackList.append(cb1)
    callbackList.append(cb2)
    callbackList.append(cb3)

    i = 1

    def func1(cb):
        nonlocal i
        assert cb() == i
        i += 1

    callbackList.forEach(func1)

    i = 1

    def func2(handle, cb):
        nonlocal i
        assert cb() == i
        i += 1
        callbackList.remove(handle)

    callbackList.forEach(func2)

    assert callbackList.empty()
Пример #15
0
from eventpy.callbacklist import CallbackList
from eventpy.eventdispatcher import EventDispatcher

# create a CallbackList
callbackList = CallbackList()
# Add a callback.
# Lambda is not required, any function or callable object is fine
callbackList.append(lambda: print("Got callback 1."))


def anotherCallback():
    print("Got callback 2.")


callbackList.append(anotherCallback)
# Invoke the callback list
callbackList()