packages feed

hs-opentelemetry-propagator-datadog-1.0.0.0: test/spec/OpenTelemetry/Propagator/DatadogSpec.hs

{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-type-defaults #-}

module OpenTelemetry.Propagator.DatadogSpec where

import qualified Data.ByteString as B
import OpenTelemetry.Internal.Trace.Id (bytesToSpanId, bytesToTraceId)
import OpenTelemetry.Propagator.Datadog
import Test.Hspec
import Test.QuickCheck


spec :: Spec
spec = do
  context "convertOpenTelemetrySpanIdToDatadogSpanId" $ do
    it "can conert values" $
      property $ \(x1, x2, x3, x4, x5, x6, x7, x8) ->
        let v =
              fromIntegral x1 * (2 ^ 8) ^ 7
                + fromIntegral x2 * (2 ^ 8) ^ 6
                + fromIntegral x3 * (2 ^ 8) ^ 5
                + fromIntegral x4 * (2 ^ 8) ^ 4
                + fromIntegral x5 * (2 ^ 8) ^ 3
                + fromIntegral x6 * (2 ^ 8) ^ 2
                + fromIntegral x7 * (2 ^ 8) ^ 1
                + fromIntegral x8
            otelSpanId =
              case bytesToSpanId (B.pack [x1, x2, x3, x4, x5, x6, x7, x8]) of
                Right sid -> sid
                Left err -> error err
        in convertOpenTelemetrySpanIdToDatadogSpanId otelSpanId == v

  context "convertOpenTelemetryTraceIdToDatadogTraceId" $ do
    it "can conert values" $
      property $ \(x1, x2, x3, x4, x5, x6, x7, x8) ->
        let v =
              fromIntegral x1 * (2 ^ 8) ^ 7
                + fromIntegral x2 * (2 ^ 8) ^ 6
                + fromIntegral x3 * (2 ^ 8) ^ 5
                + fromIntegral x4 * (2 ^ 8) ^ 4
                + fromIntegral x5 * (2 ^ 8) ^ 3
                + fromIntegral x6 * (2 ^ 8) ^ 2
                + fromIntegral x7 * (2 ^ 8) ^ 1
                + fromIntegral x8
            otelTraceId =
              case bytesToTraceId (B.pack (replicate 8 0 ++ [x1, x2, x3, x4, x5, x6, x7, x8])) of
                Right tid -> tid
                Left err -> error err
        in convertOpenTelemetryTraceIdToDatadogTraceId otelTraceId == v