on master help sign in
Fix off-by-one in buffering
57e451aMichael Xavier
katip-logzio/src/Katip/Scribes/LogzIO/HTTPS.hs+2−2
@@ -443,7 +443,7 @@ flushBuffer config mgr bulkBuffer
443-------------------------------------------------------------------------------
443-------------------------------------------------------------------------------
444newtype Bytes = Bytes
444newtype Bytes = Bytes
445 { bytes :: Int64
445 { bytes :: Int64
446− } deriving (Show, Eq, Num, Ord)
446+ } deriving (Show, Eq, Num, Ord, Bounded)
447
447
448
448
449-- | How big of a body can we send? The limit is defined
449-- | How big of a body can we send? The limit is defined
@@ -599,7 +599,7 @@ bufferItem' customMaxPayload customMaxItem maxItems verb item bulkBuffer =
599 let (encodedLine, spaceNeeded) = renderLineTruncated' customMaxItem verb item
599 let (encodedLine, spaceNeeded) = renderLineTruncated' customMaxItem verb item
600 newBytesUsed = bulkBuffer_bytesUsed bulkBuffer + spaceNeeded
600 newBytesUsed = bulkBuffer_bytesUsed bulkBuffer + spaceNeeded
601 newItemCount = bulkBuffer_itemCount bulkBuffer + 1
601 newItemCount = bulkBuffer_itemCount bulkBuffer + 1
602− in if newItemCount > maxItems || newBytesUsed > customMaxPayload
602+ in if newItemCount >= maxItems || newBytesUsed >= customMaxPayload
603 then FlushNow
603 then FlushNow
604 bulkBuffer
604 bulkBuffer
605 BulkBuffer
605 BulkBuffer
katip-logzio/test/Katip/Tests/Scribes/LogzIO/HTTPS.hs+9
@@ -223,6 +223,15 @@ bufferItemTests = testGroup "bufferItem"
223 case logAction of
223 case logAction of
224 FlushNow _ newBuffer -> bulkBuffer_itemCount newBuffer === 1
224 FlushNow _ newBuffer -> bulkBuffer_itemCount newBuffer === 1
225 _ -> pure ()
225 _ -> pure ()
226+ , testProperty "flushes when the buffer is exactly at the limit" $ property $ do
227+ maxItems <- forAll (Gen.int (Range.linear 1 20))
228+ item <- forAllWith showSimplePayloadItem genSimplePayloadItem
229+ bulkBuffer <- forAllWith inspectBulkBuffer ((\b -> b { bulkBuffer_itemCount = maxItems - 1 }) <$> genBulkBuffer)
230+ verbosity <- forAll genVerbosity
231+ let logAction = bufferItem' maxBound maxBound maxItems verbosity item bulkBuffer
232+ case logAction of
233+ FlushNow _ _ -> pure ()
234+ Buffered _ -> fail "Expected a FlushNow but got a Buffered"
226 ]
235 ]
227 where
236 where
228 assertFlushNow (FlushNow _ _) = pure ()
237 assertFlushNow (FlushNow _ _) = pure ()