packages feed

otel-effectful-1.0.0: test/Effectful/OpenTelemetry/ExporterSpec.hs

module Effectful.OpenTelemetry.ExporterSpec (spec) where

import Data.Foldable (for_)
import Effectful
import Effectful.Concurrent (runConcurrent)
import Effectful.Concurrent.STM (atomically, flushTQueue, newTQueueIO, writeTQueue)
import Effectful.Hspec
import Effectful.OpenTelemetry.Exporter qualified as Exporter
import Prelude

spec :: (IOE :> es, Hspec :> es) => Eff es ()
spec = runConcurrent do
    prop "associativity" \(xs :: [Int]) -> do
        q <- newTQueueIO
        let tag t = Exporter.atomically $ writeTQueue q . (t,)
            a = tag 'a'
            b = tag 'b'
            c = tag 'c'
            expected = [(t, x) | x <- xs, t <- "abc"]
            actual = atomically $ flushTQueue q

        Exporter.withExporter ((a <> b) <> c) $ liftIO . for_ xs
        actual `shouldReturn` expected

        Exporter.withExporter (a <> (b <> c)) $ liftIO . for_ xs
        actual `shouldReturn` expected

    prop "identity" \(xs :: [Int]) -> do
        let composeWith f = do
                q <- newTQueueIO
                let e = f $ Exporter.tqueue q
                Exporter.withExporter e $ liftIO . for_ xs
                atomically $ flushTQueue q
        composeWith id `shouldReturn` xs
        composeWith (mempty <>) `shouldReturn` xs
        composeWith (<> mempty) `shouldReturn` xs