From d9ce54ef26cabe894d0994aa2a8ab2300bc09f39 Mon Sep 17 00:00:00 2001 From: "henrika@chromium.org" Date: Wed, 7 Mar 2012 18:32:52 +0000 Subject: [PATCH] Improves flaky unit tests on Windows and Mac OS X. BUG=116961 TEST=media_unittests on Windows and Mac OS X Review URL: http://codereview.chromium.org/9625002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@125415 0039d316-1c4b-4281-b951-d872f2087c98 --- .../mac/audio_low_latency_input_mac_unittest.cc | 41 ++++++++++++++-------- .../win/audio_low_latency_input_win_unittest.cc | 37 ++++++++++++------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/media/audio/mac/audio_low_latency_input_mac_unittest.cc b/media/audio/mac/audio_low_latency_input_mac_unittest.cc index fdd170e3cbf8..6a01a3bca5b4 100644 --- a/media/audio/mac/audio_low_latency_input_mac_unittest.cc +++ b/media/audio/mac/audio_low_latency_input_mac_unittest.cc @@ -4,6 +4,7 @@ #include "base/basictypes.h" #include "base/environment.h" +#include "base/message_loop.h" #include "base/test/test_timeouts.h" #include "base/threading/platform_thread.h" #include "media/audio/audio_io.h" @@ -15,10 +16,16 @@ using ::testing::_; using ::testing::AnyNumber; -using ::testing::Between; +using ::testing::AtLeast; using ::testing::Ge; using ::testing::NotNull; +ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) { + if (++*count >= limit) { + loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); + } +} + class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { public: MOCK_METHOD4(OnData, void(AudioInputStream* stream, @@ -207,6 +214,9 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { if (!CanRunAudioTests()) return; + int count = 0; + MessageLoopForUI loop; + // Create an audio input stream which records in mono. AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); EXPECT_TRUE(ais->Open()); @@ -218,16 +228,15 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { MockAudioInputCallback sink; - // We use 10ms packets and will run the test for ~100ms. Given that the - // startup sequence takes some time, it is reasonable to expect 5-10 - // callbacks in this time period. All should contain valid packets of - // the same size. + // We use 10ms packets and will run the test until ten packets are received. + // All should contain valid packets of the same size and a valid delay + // estimate. EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, Ge(bytes_per_packet))) - .Times(Between(5, 10)); - + .Times(AtLeast(10)) + .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); ais->Start(&sink); - base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); + loop.Run(); ais->Stop(); // Verify that the sink receieves OnClose() call when calling Close(). @@ -241,6 +250,9 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { if (!CanRunAudioTests()) return; + int count = 0; + MessageLoopForUI loop; + // Create an audio input stream which records in stereo. AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); EXPECT_TRUE(ais->Open()); @@ -252,16 +264,15 @@ TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { MockAudioInputCallback sink; - // We use 10ms packets and will run the test for ~100ms. Given that the - // startup sequence takes some time, it is reasonable to expect 5-10 - // callbacks in this time period. All should contain valid packets of - // the same size. + // We use 10ms packets and will run the test until ten packets are received. + // All should contain valid packets of the same size and a valid delay + // estimate. EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, Ge(bytes_per_packet))) - .Times(Between(5, 10)); - + .Times(AtLeast(10)) + .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); ais->Start(&sink); - base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); + loop.Run(); ais->Stop(); // Verify that the sink receieves OnClose() call when calling Close(). diff --git a/media/audio/win/audio_low_latency_input_win_unittest.cc b/media/audio/win/audio_low_latency_input_win_unittest.cc index c4f6f9d35dbe..11554c77850b 100644 --- a/media/audio/win/audio_low_latency_input_win_unittest.cc +++ b/media/audio/win/audio_low_latency_input_win_unittest.cc @@ -9,6 +9,7 @@ #include "base/environment.h" #include "base/file_util.h" #include "base/memory/scoped_ptr.h" +#include "base/message_loop.h" #include "base/path_service.h" #include "base/test/test_timeouts.h" #include "base/win/scoped_com_initializer.h" @@ -21,10 +22,16 @@ using base::win::ScopedCOMInitializer; using ::testing::AnyNumber; -using ::testing::Between; +using ::testing::AtLeast; using ::testing::Gt; using ::testing::NotNull; +ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) { + if (++*count >= limit) { + loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); + } +} + class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { public: MOCK_METHOD4(OnData, void(AudioInputStream* stream, @@ -278,6 +285,9 @@ TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { if (!CanRunAudioTests(audio_manager.get())) return; + int count = 0; + MessageLoopForUI loop; + // 10 ms packet size. // Create default WASAPI input stream which records in stereo using @@ -292,16 +302,15 @@ TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * (aisw.bits_per_sample() / 8); - // We use 10ms packets and will run the test for ~100ms. Given that the - // startup sequence takes some time, it is reasonable to expect 5-12 - // callbacks in this time period. All should contain valid packets of - // the same size and a valid delay estimate. + // We use 10ms packets and will run the test until ten packets are received. + // All should contain valid packets of the same size and a valid delay + // estimate. EXPECT_CALL(sink, OnData( ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) - .Times(Between(5, 10)); - + .Times(AtLeast(10)) + .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); ais->Start(&sink); - base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); + loop.Run(); ais->Stop(); // Store current packet size (to be used in the subsequent tests). @@ -313,6 +322,7 @@ TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { // 20 ms packet size. + count = 0; ais = aisw.Create(2 * samples_per_packet_10ms); EXPECT_TRUE(ais->Open()); bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * @@ -320,9 +330,10 @@ TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { EXPECT_CALL(sink, OnData( ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) - .Times(Between(5, 10)); + .Times(AtLeast(10)) + .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); ais->Start(&sink); - base::PlatformThread::Sleep(2 * TestTimeouts::tiny_timeout()); + loop.Run(); ais->Stop(); EXPECT_CALL(sink, OnClose(ais)) @@ -331,6 +342,7 @@ TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { // 5 ms packet size. + count = 0; ais = aisw.Create(samples_per_packet_10ms / 2); EXPECT_TRUE(ais->Open()); bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * @@ -338,9 +350,10 @@ TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { EXPECT_CALL(sink, OnData( ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) - .Times(Between(2 * 5, 2 * 10)); + .Times(AtLeast(10)) + .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); ais->Start(&sink); - base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); + loop.Run(); ais->Stop(); EXPECT_CALL(sink, OnClose(ais)) -- 2.11.4.GIT