11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33import gc
4- import json
54import pytest
65import threading
76import time
@@ -1403,6 +1402,7 @@ def __init__(self, config):
14031402 # Test __len__() - should return 0 for closed producer (safe, no crash)
14041403 assert len (producer ) == 0
14051404
1405+
14061406def test_producer_close ():
14071407 """
14081408 Ensures the producer close can be requested on demand
@@ -1414,9 +1414,11 @@ def test_producer_close():
14141414 'message.timeout.ms' : 10
14151415 }
14161416 producer = Producer (conf )
1417- msg = {"test" : "test" }
1418- producer .produce (json .dumps (msg ))
1417+ producer .produce ('mytopic' , value = 'somedata' , key = 'a key' )
14191418 assert producer .close (), "The producer could not be closed on demand"
1419+ # Ensure no messages remain in the flush buffer after close
1420+ assert len (producer ) == 0
1421+
14201422
14211423def test_producer_close_with_timeout ():
14221424 """
@@ -1429,6 +1431,7 @@ def test_producer_close_with_timeout():
14291431 'message.timeout.ms' : 10
14301432 }
14311433 producer = Producer (conf )
1432- msg = {"test" : "test" }
1433- producer .produce (json .dumps (msg ))
1434- assert producer .close (0.1 ), "The producer could not be closed on demand with timeout"
1434+ producer .produce ('mytopic' , value = 'somedata' , key = 'a key' )
1435+ assert producer .close (0.1 ), "The producer could not be closed on demand with timeout"
1436+ # Ensure no messages remain in the flush buffer after close
1437+ assert len (producer ) == 0
0 commit comments