Skip to content

Commit

Permalink
PGListener.unsubscribe is in fact not broken, it was the test that wa…
Browse files Browse the repository at this point in the history
…s wrong
  • Loading branch information
mpscholten committed Feb 5, 2022
1 parent ceb0351 commit 3c77188
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions IHP/PGListener.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ subscribeJSON channel callback pgListener = subscribe channel callback' pgListen
unsubscribe :: Subscription -> PGListener -> IO ()
unsubscribe subscription@(Subscription { .. }) pgListener = do
let
deleteById :: UUID -> [Subscription] -> [Subscription]
deleteById id = List.deleteBy (\a b -> get #id a == get #id b) subscription
modifyIORef' (get #subscriptions pgListener) (HashMap.adjust (deleteById id) channel)
deleteById :: [Subscription] -> [Subscription]
deleteById = List.deleteBy (\a b -> get #id a == get #id b) subscription
modifyIORef' (get #subscriptions pgListener) (HashMap.adjust deleteById channel)
pure ()

-- | Runs a @LISTEN ..;@ statements on the postgres connection, if not already listening on that channel
Expand Down
8 changes: 5 additions & 3 deletions Test/PGListenerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ tests = do
it "should add a subscriber" do
pgListener <- PGListener.init modelContext

subscriptionsCount <- HashMap.size <$> readIORef (pgListener |> get #subscriptions)
subscriptionsCount <- length . concat . HashMap.elems <$> readIORef (pgListener |> get #subscriptions)
subscriptionsCount `shouldBe` 0

let didInsertRecordCallback notification = pure ()

pgListener |> PGListener.subscribe "did_insert_record" didInsertRecordCallback

subscriptionsCount <- HashMap.size <$> readIORef (pgListener |> get #subscriptions)
subscriptionsCount <- length . concat . HashMap.elems <$> readIORef (pgListener |> get #subscriptions)
subscriptionsCount `shouldBe` 1

describe "unsubscribe" do
Expand All @@ -36,5 +36,7 @@ tests = do
subscription <- pgListener |> PGListener.subscribe "did_insert_record" (const (pure ()))
pgListener |> PGListener.unsubscribe subscription

subscriptionsCount <- HashMap.size <$> readIORef (pgListener |> get #subscriptions)
subscriptions <- readIORef (pgListener |> get #subscriptions)
subscriptionsCount <- length . concat . HashMap.elems <$> readIORef (pgListener |> get #subscriptions)

subscriptionsCount `shouldBe` 0

0 comments on commit 3c77188

Please sign in to comment.