def test_closing_resource_context(self): from wiringsamples import resourceclosing resourceclosing.Service.reset_counter() service = resourceclosing.Service() container = resourceclosing.Container() container.wire(modules=[resourceclosing]) self.addCleanup(container.unwire) result_1 = resourceclosing.test_function(service=service) self.assertIs(result_1, service) self.assertEqual(result_1.init_counter, 0) self.assertEqual(result_1.shutdown_counter, 0) result_2 = resourceclosing.test_function(service=service) self.assertIs(result_2, service) self.assertEqual(result_2.init_counter, 0) self.assertEqual(result_2.shutdown_counter, 0)
def test_closing_resource_bypass_marker_injection(self): from wiringsamples import resourceclosing resourceclosing.Service.reset_counter() container = resourceclosing.Container() container.wire(modules=[resourceclosing]) self.addCleanup(container.unwire) result_1 = resourceclosing.test_function( service=Closing[Provide[resourceclosing.Container.service]], ) self.assertIsInstance(result_1, resourceclosing.Service) self.assertEqual(result_1.init_counter, 1) self.assertEqual(result_1.shutdown_counter, 1) result_2 = resourceclosing.test_function( service=Closing[Provide[resourceclosing.Container.service]], ) self.assertIsInstance(result_2, resourceclosing.Service) self.assertEqual(result_2.init_counter, 2) self.assertEqual(result_2.shutdown_counter, 2) self.assertIsNot(result_1, result_2)