| // Copyright 2023 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "stat_cache.h" |
| |
| #include <stdio.h> |
| |
| #include "test.h" |
| #include "util.h" |
| |
| namespace { |
| |
| void WriteFile(const std::string& path, const std::string& content) { |
| FILE* f = fopen(path.c_str(), "wb"); |
| if (!f) |
| ErrnoFatal("fopen", path.c_str()); |
| |
| size_t ret = fwrite(content.c_str(), content.size(), 1, f); |
| if (ret != 1) |
| ErrnoFatal("Could not write to %s", path.c_str()); |
| fclose(f); |
| } |
| |
| void RemoveFile(const std::string& path) { |
| ::remove(path.c_str()); |
| } |
| |
| } // namespace |
| |
| TEST(StatCache, CheckMissingTimestamp) { |
| ScopedTempDir temp_dir; |
| temp_dir.CreateAndEnter("StatCacheTest"); |
| auto top_dir = GetCurrentDir(); |
| |
| StatCache cache; |
| cache.Enable(true); |
| |
| std::string err; |
| TimeStamp t = cache.Stat(top_dir + "/foo", &err); |
| EXPECT_EQ(0, t); |
| EXPECT_TRUE(err.empty()); |
| |
| t = cache.Stat(top_dir + "/bar", &err); |
| EXPECT_EQ(0, t); |
| EXPECT_TRUE(err.empty()); |
| |
| // Try foo again, just in case. |
| t = cache.Stat(top_dir + "/foo", &err); |
| EXPECT_EQ(0, t); |
| EXPECT_TRUE(err.empty()); |
| } |
| |
| TEST(StatCache, CheckExistingTimestamp) { |
| ScopedTempDir temp_dir; |
| temp_dir.CreateAndEnter("StatCacheTest"); |
| auto top_dir = GetCurrentDir(); |
| |
| StatCache cache; |
| cache.Enable(true); |
| |
| std::string foo_path = top_dir + "/foo"; |
| |
| WriteFile(foo_path, "foo!!"); |
| |
| std::string err; |
| TimeStamp t = cache.Stat(foo_path, &err); |
| EXPECT_GE(t, 0); |
| EXPECT_TRUE(err.empty()); |
| } |
| |
| TEST(StatCache, CheckCreatedTimestamp) { |
| ScopedTempDir temp_dir; |
| temp_dir.CreateAndEnter("StatCacheTest"); |
| auto top_dir = GetCurrentDir(); |
| |
| StatCache cache; |
| cache.Enable(true); |
| |
| std::string foo_path = top_dir + "/foo"; |
| |
| std::string err; |
| TimeStamp t = cache.Stat(foo_path, &err); |
| EXPECT_EQ(0, t); |
| EXPECT_TRUE(err.empty()); |
| |
| WriteFile(foo_path, "foo!!"); |
| cache.Sync(); |
| |
| TimeStamp t2 = cache.Stat(foo_path, &err); |
| EXPECT_NE(0, t2); |
| EXPECT_TRUE(err.empty()); |
| } |
| |
| TEST(StatCache, CheckDeletedTimestamp) { |
| ScopedTempDir temp_dir; |
| temp_dir.CreateAndEnter("StatCacheTest"); |
| auto top_dir = GetCurrentDir(); |
| |
| StatCache cache; |
| cache.Enable(true); |
| |
| std::string foo_path = top_dir + "/foo"; |
| WriteFile(foo_path, "foo!!"); |
| |
| std::string err; |
| TimeStamp t = cache.Stat(foo_path, &err); |
| EXPECT_NE(0, t); |
| EXPECT_TRUE(err.empty()); |
| |
| RemoveFile(foo_path); |
| |
| cache.Sync(); |
| |
| TimeStamp t2 = cache.Stat(foo_path, &err); |
| EXPECT_EQ(0, t2); |
| EXPECT_TRUE(err.empty()); |
| } |