|
5 | 5 | require "active_support/test_case"
|
6 | 6 | require "rails/rack/logger"
|
7 | 7 | require "logger"
|
| 8 | +require "active_support/log_subscriber/test_helper" |
8 | 9 |
|
9 | 10 | module Rails
|
10 | 11 | module Rack
|
11 | 12 | class LoggerTest < ActiveSupport::TestCase
|
| 13 | + include ActiveSupport::LogSubscriber::TestHelper |
| 14 | + |
12 | 15 | class TestLogger < Rails::Rack::Logger
|
13 | 16 | NULL = ::Logger.new File::NULL
|
14 | 17 |
|
@@ -43,16 +46,16 @@ def finish(name, id, payload)
|
43 | 46 | end
|
44 | 47 | end
|
45 | 48 |
|
46 |
| - attr_reader :subscriber, :notifier |
| 49 | + attr_reader :subscriber |
47 | 50 |
|
48 | 51 | def setup
|
| 52 | + super |
49 | 53 | @subscriber = Subscriber.new
|
50 |
| - @notifier = ActiveSupport::Notifications.notifier |
51 |
| - @subscription = notifier.subscribe "request.action_dispatch", subscriber |
| 54 | + @subscription = ActiveSupport::Notifications.notifier.subscribe "request.action_dispatch", subscriber |
52 | 55 | end
|
53 | 56 |
|
54 | 57 | def teardown
|
55 |
| - notifier.unsubscribe @subscription |
| 58 | + ActiveSupport::Notifications.notifier.unsubscribe @subscription |
56 | 59 | end
|
57 | 60 |
|
58 | 61 | def test_notification
|
@@ -90,6 +93,32 @@ def test_logger_does_not_mutate_app_return
|
90 | 93 | end
|
91 | 94 | end
|
92 | 95 | end
|
| 96 | + |
| 97 | + def test_logger_is_flushed_after_request_finished |
| 98 | + logger_middleware = TestLogger.new { } |
| 99 | + |
| 100 | + flush_count_in_request_event = nil |
| 101 | + block_sub = @notifier.subscribe "request.action_dispatch" do |_event| |
| 102 | + flush_count_in_request_event = ActiveSupport::LogSubscriber.logger.flush_count |
| 103 | + end |
| 104 | + |
| 105 | + # Assert that we don't get a logger flush when we finish the response headers |
| 106 | + response_body = nil |
| 107 | + assert_no_difference("ActiveSupport::LogSubscriber.logger.flush_count") do |
| 108 | + response_body = logger_middleware.call("REQUEST_METHOD" => "GET").last |
| 109 | + end |
| 110 | + |
| 111 | + # Assert that we _do_ get a logger flush when we finish the response body |
| 112 | + assert_difference("ActiveSupport::LogSubscriber.logger.flush_count") do |
| 113 | + response_body.close |
| 114 | + end |
| 115 | + |
| 116 | + # And that the flush happens _after_ any LogSubscribers etc get run. |
| 117 | + flush_count = ActiveSupport::LogSubscriber.logger.flush_count |
| 118 | + assert_equal(1, flush_count - flush_count_in_request_event, "flush_all! should happen after event") |
| 119 | + ensure |
| 120 | + @notifier.unsubscribe block_sub |
| 121 | + end |
93 | 122 | end
|
94 | 123 | end
|
95 | 124 | end
|
0 commit comments