on master help sign in
Fix off-by-one in buffering

If we've filled the buffer on a push, we shouldn't wait until the next push or tick to flush it.

commit57e451a5a8d858c9a8b04123a8ffc14b878283fd authorMichael Xavier <michael@michaelxavier.net> dateWed 13 Feb 2019, 08:43 (8 years ago) parent22c2c0f
2 files changed+11−2
katip-logzio/src/Katip/Scribes/LogzIO/HTTPS.hs+2−2 katip-logzio/test/Katip/Tests/Scribes/LogzIO/HTTPS.hs+9
katip-logzio/src/Katip/Scribes/LogzIO/HTTPS.hs+2−2
@@ -443,7 +443,7 @@ flushBuffer config mgr bulkBuffer
443443-------------------------------------------------------------------------------
444444newtype Bytes = Bytes
445445 { bytes :: Int64
446− } deriving (Show, Eq, Num, Ord)
446+ } deriving (Show, Eq, Num, Ord, Bounded)
447447
448448
449449-- | How big of a body can we send? The limit is defined
@@ -599,7 +599,7 @@ bufferItem' customMaxPayload customMaxItem maxItems verb item bulkBuffer =
599599 let (encodedLine, spaceNeeded) = renderLineTruncated' customMaxItem verb item
600600 newBytesUsed = bulkBuffer_bytesUsed bulkBuffer + spaceNeeded
601601 newItemCount = bulkBuffer_itemCount bulkBuffer + 1
602− in if newItemCount > maxItems || newBytesUsed > customMaxPayload
602+ in if newItemCount >= maxItems || newBytesUsed >= customMaxPayload
603603 then FlushNow
604604 bulkBuffer
605605 BulkBuffer
katip-logzio/test/Katip/Tests/Scribes/LogzIO/HTTPS.hs+9
@@ -223,6 +223,15 @@ bufferItemTests = testGroup "bufferItem"
223223 case logAction of
224224 FlushNow _ newBuffer -> bulkBuffer_itemCount newBuffer === 1
225225 _ -> 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"
226235 ]
227236 where
228237 assertFlushNow (FlushNow _ _) = pure ()