Skip to content

Commit 000a17c

Browse files
Fixed 'I'm an idiot' deadlock in text-to-speech unit tests.
1 parent 7aed8c3 commit 000a17c

File tree

1 file changed

+73
-71
lines changed

1 file changed

+73
-71
lines changed

aws-cpp-sdk-text-to-speech-tests/TextToSpeechManagerTests.cpp

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -30,71 +30,71 @@ static const char* ALLOC_TAG = "TextToSpeechManagerTests";
3030

3131
class MockPCMDriver : public PCMOutputDriver
3232
{
33-
public:
34-
MockPCMDriver() : m_primeCalledCount(0), m_flushCalledCount(0), m_mockWriteResponse(false) {}
35-
36-
bool WriteBufferToDevice(const unsigned char* buffer, size_t size) override
37-
{
38-
m_buffersWritten.push_back(ByteBuffer(buffer, size));
39-
return m_mockWriteResponse;
40-
}
41-
42-
void MockWriteResponse(bool response)
43-
{
44-
m_mockWriteResponse = response;
45-
}
46-
47-
const Aws::Vector<ByteBuffer>& GetWrittenBuffers() const
48-
{
49-
return m_buffersWritten;
50-
}
51-
52-
Aws::Vector<DeviceInfo> EnumerateDevices() const override
53-
{
54-
return m_devices;
55-
}
56-
57-
void AddDevice(const DeviceInfo& device)
58-
{
59-
m_devices.push_back(device);
60-
}
61-
62-
void SetActiveDevice(const DeviceInfo& device, const CapabilityInfo& caps) override
63-
{
64-
m_activeDevice = device;
65-
m_selectedCaps = caps;
66-
}
67-
68-
const DeviceInfo& GetActiveDevice() const
69-
{
70-
return m_activeDevice;
71-
}
72-
73-
const CapabilityInfo& GetActiveCaps() const
74-
{
75-
return m_selectedCaps;
76-
}
77-
78-
const char* GetName() const override
79-
{
80-
return MOCK_DRIVER_NAME;
81-
}
82-
83-
size_t GetPrimeCalledCount() const
84-
{
85-
return m_primeCalledCount;
86-
}
87-
88-
void Prime() override
89-
{
90-
m_primeCalledCount++;
91-
}
92-
93-
size_t GetFlushCalledCount() const
94-
{
95-
return m_primeCalledCount;
96-
}
97-
33+
public:
34+
MockPCMDriver() : m_primeCalledCount(0), m_flushCalledCount(0), m_mockWriteResponse(false) {}
35+
36+
bool WriteBufferToDevice(const unsigned char* buffer, size_t size) override
37+
{
38+
m_buffersWritten.push_back(ByteBuffer(buffer, size));
39+
return m_mockWriteResponse;
40+
}
41+
42+
void MockWriteResponse(bool response)
43+
{
44+
m_mockWriteResponse = response;
45+
}
46+
47+
const Aws::Vector<ByteBuffer>& GetWrittenBuffers() const
48+
{
49+
return m_buffersWritten;
50+
}
51+
52+
Aws::Vector<DeviceInfo> EnumerateDevices() const override
53+
{
54+
return m_devices;
55+
}
56+
57+
void AddDevice(const DeviceInfo& device)
58+
{
59+
m_devices.push_back(device);
60+
}
61+
62+
void SetActiveDevice(const DeviceInfo& device, const CapabilityInfo& caps) override
63+
{
64+
m_activeDevice = device;
65+
m_selectedCaps = caps;
66+
}
67+
68+
const DeviceInfo& GetActiveDevice() const
69+
{
70+
return m_activeDevice;
71+
}
72+
73+
const CapabilityInfo& GetActiveCaps() const
74+
{
75+
return m_selectedCaps;
76+
}
77+
78+
const char* GetName() const override
79+
{
80+
return MOCK_DRIVER_NAME;
81+
}
82+
83+
size_t GetPrimeCalledCount() const
84+
{
85+
return m_primeCalledCount;
86+
}
87+
88+
void Prime() override
89+
{
90+
m_primeCalledCount++;
91+
}
92+
93+
size_t GetFlushCalledCount() const
94+
{
95+
return m_primeCalledCount;
96+
}
97+
9898
void Flush() override
9999
{
100100
m_flushCalledCount++;
@@ -320,14 +320,15 @@ TEST(TextToSpeechManagerTests, TestSynthResponseAndOutput)
320320
const char* REQUEST_TEXT = "Blah blah blah";
321321

322322
SendTextCompletedHandler handler = [&](const char* text, const SynthesizeSpeechOutcome&, bool sent)
323-
{
324-
EXPECT_STREQ(REQUEST_TEXT, text);
325-
EXPECT_TRUE(sent);
326-
semaphore.notify_all();
323+
{
324+
std::lock_guard<std::mutex> lockGuard(lock);
325+
EXPECT_STREQ(REQUEST_TEXT, text);
326+
EXPECT_TRUE(sent);
327+
semaphore.notify_all();
327328
};
328329

329-
manager.SendTextToOutputDevice(REQUEST_TEXT, handler);
330330
std::unique_lock<std::mutex> locker(lock);
331+
manager.SendTextToOutputDevice(REQUEST_TEXT, handler);
331332
semaphore.wait(locker);
332333

333334
auto capturedRequest = pollyClient->GetCapturedSynthesizeSpeech();
@@ -381,13 +382,14 @@ TEST(TextToSpeechManagerTests, TestSynthRequestFailedAndNoOutput)
381382

382383
SendTextCompletedHandler handler = [&](const char* text, const SynthesizeSpeechOutcome&, bool sent)
383384
{
385+
std::lock_guard<std::mutex> lockGuard(lock);
384386
EXPECT_STREQ(REQUEST_TEXT, text);
385387
EXPECT_FALSE(sent);
386388
semaphore.notify_all();
387389
};
388390

389-
manager.SendTextToOutputDevice(REQUEST_TEXT, handler);
390391
std::unique_lock<std::mutex> locker(lock);
392+
manager.SendTextToOutputDevice(REQUEST_TEXT, handler);
391393
semaphore.wait(locker);
392394

393395
auto capturedRequest = pollyClient->GetCapturedSynthesizeSpeech();

0 commit comments

Comments
 (0)