Skip to content

Commit

Permalink
Handle double start and stop calls
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Dec 5, 2022
1 parent 678ab05 commit ed00285
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
14 changes: 9 additions & 5 deletions libp2p/services/autonatservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ proc register(service: AutonatService, switch: Switch, interval: Duration) {.asy
heartbeat "Register AutonatService run", interval:
await service.run(switch)

method setup*(self: AutonatService, switch: Switch) {.async.} =
if self.scheduleInterval.isSome:
self.registerLoop = register(self, switch, self.scheduleInterval.get())
method setup*(self: AutonatService, switch: Switch): Future[bool] {.async.} =
let hasBeenSettedUp = await procCall Service(self).setup(switch)
if hasBeenSettedUp and self.scheduleInterval.isSome:
self.registerLoop = register(self, switch, self.scheduleInterval.get())
return hasBeenSettedUp

method run*(self: AutonatService, switch: Switch) {.async, public.} =
await askPeersInAddressBook(self, switch)

method stop*(self: AutonatService, switch: Switch) {.async, public.} =
if self.scheduleInterval.isSome and self.registerLoop != nil:
method stop*(self: AutonatService, switch: Switch): Future[bool] {.async, public.} =
let hasBeenStopped = await procCall Service(self).stop(switch)
if hasBeenStopped and self.scheduleInterval.isSome and self.registerLoop != nil:
self.registerLoop.cancel()
self.registerLoop = nil
return hasBeenStopped

proc onNewStatuswithMaxConfidence*(self: AutonatService, f: NewStatusHandler) =
self.newStatusHandler = f
24 changes: 17 additions & 7 deletions libp2p/switch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,25 @@ type
services*: seq[Service]

Service* = ref object of RootObj
inUse: bool

# proc new*(T: typedesc[Service], scheduleInterval: Option[Duration] = none(Duration)) =
# T (scheduleInterval: scheduleInterval)

method setup*(self: Service, switch: Switch) {.base, async, gcsafe, public.} = discard
method setup*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe, public.} =
if self.inUse:
warn "service setup has already been called"
return false
self.inUse = true
return true

method run*(self: Service, switch: Switch) {.base, async, gcsafe, public.} = discard
method run*(self: Service, switch: Switch) {.base, async, gcsafe, public.} =
doAssert(false, "Not implemented!")

method stop*(self: Service, switch: Switch) {.base, async, gcsafe, public.} = discard
method stop*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe, public.} =
if not self.inUse:
warn "service is already stopped"
return false
self.inUse = false
return true

proc addConnEventHandler*(s: Switch,
handler: ConnEventHandler,
Expand Down Expand Up @@ -308,7 +318,7 @@ proc stop*(s: Switch) {.async, public.} =
a.cancel()

for service in s.services:
await service.stop(s)
discard await service.stop(s)

await s.ms.stop()

Expand Down Expand Up @@ -348,7 +358,7 @@ proc start*(s: Switch) {.async, gcsafe, public.} =
await s.ms.start()

for service in s.services:
await service.setup(s)
discard await service.setup(s)

s.started = true

Expand Down
30 changes: 8 additions & 22 deletions tests/testautonatservice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,15 @@ suite "Autonat Service":

await allFuturesThrowing(switch1.stop(), switch2.stop(), switch3.stop(), switch4.stop())

# asyncTest "IPFS Hope Punching test":
# let switch1 = createAutonatSwitch()
asyncTest "Autonat Service setup and stop twice":

# switch1.addService(HPService.new())
let switch = createSwitch()
let autonatService = AutonatService.new(AutonatStub.new(expectedDials = 0), newRng(), some(1.seconds))

# await switch1.start()
check (await autonatService.setup(switch)) == true
check (await autonatService.setup(switch)) == false

# asyncSpawn switch1.connect(
# PeerId.init("QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").get(),
# @[MultiAddress.init("/ip4/139.178.91.71/tcp/4001").get()]
# )
check (await autonatService.stop(switch)) == true
check (await autonatService.stop(switch)) == false

# asyncSpawn switch1.connect(
# PeerId.init("QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt").get(),
# @[MultiAddress.init("/ip4/145.40.118.135/tcp/4001").get()]
# )

# asyncSpawn switch1.connect(
# PeerId.init("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ").get(),
# @[MultiAddress.init("/ip4/104.131.131.82/tcp/4001").get()]
# )

# await sleepAsync(20.seconds)

# await allFuturesThrowing(
# switch1.stop())
await allFuturesThrowing(switch.stop())

0 comments on commit ed00285

Please sign in to comment.