Skip to content

Commit

Permalink
Improved Test (IT FAILS)
Browse files Browse the repository at this point in the history
  • Loading branch information
KKQ-KKQ committed Feb 6, 2024
1 parent 84e06b5 commit 1bdd023
Showing 1 changed file with 187 additions and 13 deletions.
200 changes: 187 additions & 13 deletions tests/FilePoolT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include <chrono>
#include <thread>
#include <memory>
#include <functional>
#include <atomic>
using namespace Catch::literals;

static void WAIT()
static void WAIT(int ms)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}

TEST_CASE("[FilePool] Shared samples")
Expand Down Expand Up @@ -56,10 +58,7 @@ TEST_CASE("[FilePool] Shared samples")
CHECK(synth2->getResources().getFilePool().getGlobalNumPreloadedSamples() == 1);
CHECK(synth3->getResources().getFilePool().getGlobalNumPreloadedSamples() == 1);

sfz::AudioBuffer<float> buffer1 { 2, 256 };
sfz::AudioBuffer<float> buffer2 { 2, 256 };
std::vector<float> tmp1 (buffer1.getNumFrames());
std::vector<float> tmp2 (buffer2.getNumFrames());
sfz::AudioBuffer<float> buffer { 2, 256 };

synth2->loadSfzFile("");

Expand All @@ -85,16 +84,34 @@ TEST_CASE("[FilePool] Shared samples")
CHECK(synth2->getResources().getFilePool().getGlobalNumPreloadedSamples() == 1);
CHECK(synth3->getResources().getFilePool().getGlobalNumPreloadedSamples() == 1);

#if 1
return;
// The following process cause a crash on GitHub
#endif
// Crash test
synth1->noteOn(0, 60, 100);
for (unsigned i = 0; i < 100; ++i) {
synth1->renderBlock(buffer);
synth2->renderBlock(buffer);
WAIT(10);
}

synth2->noteOn(0, 60, 100);
for (unsigned i = 0; i < 100; ++i) {
synth1->renderBlock(buffer);
synth2->renderBlock(buffer);
WAIT(10);
}

synth1->loadSfzFile("");
synth1->noteOff(0, 60, 100);
for (unsigned i = 0; i < 100; ++i) {
synth1->renderBlock(buffer1);
WAIT();
synth1->renderBlock(buffer);
synth2->renderBlock(buffer);
WAIT(10);
}

synth1->loadSfzFile("");
synth1->allSoundOff();
for (unsigned i = 0; i < 100; ++i) {
synth1->renderBlock(buffer);
synth2->renderBlock(buffer);
WAIT(10);
}

CHECK(synth1->getNumPreloadedSamples() == 0);
Expand Down Expand Up @@ -133,4 +150,161 @@ TEST_CASE("[FilePool] Shared samples")
CHECK(synth2->getResources().getFilePool().getGlobalNumPreloadedSamples() == 2);
CHECK(synth3->getResources().getFilePool().getGlobalNumPreloadedSamples() == 2);

// Release
synth1.reset();
synth2.reset();
synth3.reset();
}

TEST_CASE("[FilePool] WaitForInitialize")
{
struct TestThread {
TestThread(std::function<void()> execution) : execution(execution) {}
void job() noexcept
{
execution();
done = true;
}
~TestThread()
{
if (done) {
thread.join();
}
}

bool done { false };
std::thread thread { &TestThread::job, this };
std::function<void()> execution;
};
{
sfz::FileData fileData1;
sfz::FileData fileData2;
bool done = false;
TestThread thread {
[&fileData1, &done](){
fileData1.waitForInitialize();
done = true;
}
};
WAIT(300);
fileData1.initWith(sfz::FileData::Status::Invalid, std::move(fileData2));
WAIT(1000);
CHECK(done);
}
}

TEST_CASE("[FilePool] Crash Tests")
{
const unsigned synthCount = 100;

struct TestSynthThread {
TestSynthThread()
{
synth.setSamplesPerBlock(256);
}

~TestSynthThread()
{
running = false;
std::error_code ec;
semBarrier.post(ec);
ASSERT(!ec);
thread.join();
}

void job() noexcept
{
semBarrier.wait();
synth.loadSfzFile(fs::current_path() / "tests/TestFiles/looped_regions.sfz");
execution();
if (running) {
while (semBarrier.wait(), running) {
synth.renderBlock(buffer);
execution();
}
}
}

void noteOn()
{
synth.noteOn(0, 60, 100);
}

void noteOff()
{
synth.noteOff(0, 60, 100);
}

void trigger()
{
std::error_code ec;
semBarrier.post(ec);
ASSERT(!ec);
}

sfz::AudioBuffer<float> buffer { 2, 256 };
sfz::Synth synth;
std::thread thread { &TestSynthThread::job, this };
RTSemaphore semBarrier { 0 };
bool running { true };
std::function<void()> execution;
};

std::unique_ptr<TestSynthThread[]> synthThreads { new TestSynthThread[synthCount]() };

std::atomic<int> finishCount { 0 };
auto countFunc = [&finishCount]() {
++finishCount;
};
for (unsigned i = 0; i < synthCount; ++i) {
synthThreads[i].execution = countFunc;
}

finishCount = 0;
for (unsigned j = 0; j < synthCount; ++j) {
synthThreads[j].trigger();
}
int count = 0;
while (finishCount != synthCount) {
CHECK(++count < 65536);
WAIT(100);
}


for (unsigned i = 0; i < synthCount; ++i) {
CHECK(synthThreads[i].synth.getNumPreloadedSamples() == 1);
CHECK(synthThreads[i].synth.getResources().getFilePool().getActualNumPreloadedSamples() == 1);
CHECK(synthThreads[i].synth.getResources().getFilePool().getGlobalNumPreloadedSamples() == 1);
}

for (unsigned i = 0; i < synthCount; ++i) {
synthThreads[i].noteOn();
}
for (unsigned i = 0; i < 100; ++i) {
finishCount = 0;
for (unsigned j = 0; j < synthCount; ++j) {
synthThreads[j].trigger();
}
while (finishCount != synthCount) {
WAIT(10);
}
}

for (unsigned i = 0; i < synthCount; ++i) {
synthThreads[i].noteOff();
}
for (unsigned i = 0; i < 100; ++i) {
finishCount = 0;
for (unsigned j = 0; j < synthCount; ++j) {
synthThreads[j].trigger();
}
while (finishCount != synthCount) {
WAIT(10);
}
}

std::unique_ptr<sfz::Synth> synth1 { new sfz::Synth() };
synthThreads.reset();

CHECK(synth1->getResources().getFilePool().getGlobalNumPreloadedSamples() == 0);
}

0 comments on commit 1bdd023

Please sign in to comment.