Skip to content

Commit 7062261

Browse files
author
Pietro Albini
committed
Fix shared memory initializators not working (fixes #20)
Also added some tests for Component.add_shared_memory_initializer, in order to prevent this issue in the future. This is a regression introduced in 350a78a.
1 parent 9c993a8 commit 7062261

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

botogram/hooks.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ class ProcessMessageHook(Hook):
6868

6969
class SharedMemoryInitializerHook(Hook):
7070
"""Underlying hook for @bot.init_shared_memory"""
71-
pass
71+
72+
def call(self, memory):
73+
return self.func(memory)
7274

7375

7476
class NoCommandsHook(Hook):

botogram/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def apply_inits(self, component, memory):
137137
return
138138

139139
for init in self._inits[component]:
140-
init(memory)
140+
init.call(memory)
141141

142142
def switch_driver(self, driver=None):
143143
"""Use another driver for this shared memory"""

tests/test_components.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,27 @@ def sample3(chat, message, args):
259259
assert sample3_processed == True
260260

261261

262+
def test_add_shared_memory_initializer(bot, sample_update):
263+
expected = "test"
264+
actual = None
265+
266+
def initializer(shared):
267+
shared["test"] = expected
268+
269+
def command(bot, shared):
270+
nonlocal actual
271+
actual = shared["test"]
272+
273+
comp = botogram.Component("test")
274+
comp.add_shared_memory_initializer(initializer)
275+
comp.add_process_message_hook(command)
276+
277+
bot.use(comp)
278+
bot.process(sample_update)
279+
280+
assert actual == expected
281+
282+
262283
def test_add_timer(bot):
263284
global_bot = bot
264285

tests/test_shared.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pickle
99

1010
import botogram.shared
11+
import botogram.hooks
1112

1213

1314
def test_shared_memory_creation():
@@ -37,8 +38,12 @@ def init1(shared):
3738
def init2(shared):
3839
shared["b"] = 1
3940

40-
shared.register_inits_list("comp1", [init1, init2])
41-
shared.register_inits_list("comp2", [init1])
41+
comp = botogram.Component()
42+
init1_hook = botogram.hooks.SharedMemoryInitializerHook(init1, comp)
43+
init2_hook = botogram.hooks.SharedMemoryInitializerHook(init2, comp)
44+
45+
shared.register_inits_list("comp1", [init1_hook, init2_hook])
46+
shared.register_inits_list("comp2", [init1_hook])
4247

4348
memory1 = shared.of("bot1", "comp1")
4449
memory2 = shared.of("bot1", "comp2")

0 commit comments

Comments
 (0)