| 31 | namespace { |
| 32 | |
| 33 | TEST(FfmpegLibTest, TestTempDirectoryThreading) { |
| 34 | // Testing a fix for a bug that allowed different threads to create |
| 35 | // conflicting temp files. |
| 36 | // See github.com/tensorflow/tensorflow/issues/5804 for details. |
| 37 | const int32 kNumThreads = 10; |
| 38 | const int32 kNumWorkItems = 10000; |
| 39 | static constexpr size_t kStringsPerItem = 100; |
| 40 | Env* environment = Env::Default(); |
| 41 | thread::ThreadPool pool(environment, "test", kNumThreads); |
| 42 | |
| 43 | mutex mu; |
| 44 | std::vector<string> temp_filenames; |
| 45 | temp_filenames.reserve(kNumWorkItems * kStringsPerItem); |
| 46 | |
| 47 | // Queue a large number of work items for the threads to process. Each work |
| 48 | // item creates a temp file and then deletes it. |
| 49 | for (int i = 0; i < kNumWorkItems; ++i) { |
| 50 | pool.Schedule([&mu, &temp_filenames, environment]() { |
| 51 | std::array<string, kStringsPerItem> buffer; |
| 52 | for (int32 j = 0; j < kStringsPerItem; ++j) { |
| 53 | buffer[j] = io::GetTempFilename("mp3"); |
| 54 | TF_QCHECK_OK(environment->DeleteFile(buffer[j])); |
| 55 | } |
| 56 | mutex_lock l(mu); |
| 57 | for (const auto& fn : buffer) { |
| 58 | temp_filenames.push_back(fn); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | // Wait until all work items are complete. |
| 64 | while (true) { |
| 65 | mutex_lock l(mu); |
| 66 | if (temp_filenames.size() == kNumWorkItems * kStringsPerItem) { |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Check that no duplicates are created. |
| 72 | std::set<string> unique_filenames; |
| 73 | mutex_lock l(mu); |
| 74 | for (const auto& fn : temp_filenames) { |
| 75 | ASSERT_TRUE(unique_filenames.insert(fn).second); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | } // namespace ffmpeg |
nothing calls this directly
no test coverage detected