Skip to content

Commit

Permalink
Change tests to use new server time method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsimantov committed Jan 23, 2024
1 parent 7c57d96 commit c0afefa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 55 deletions.
30 changes: 2 additions & 28 deletions database/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ class FirebaseDatabaseTest : public FirebaseTest {
// Shut down Firebase Database.
void TerminateDatabase();

int64_t GetRemoteTimeInMilliseconds();

firebase::database::DatabaseReference CreateWorkingPath(
bool suppress_cleanup = false);

Expand Down Expand Up @@ -368,30 +366,6 @@ firebase::database::DatabaseReference FirebaseDatabaseTest::CreateWorkingPath(
return ref;
}

int64_t FirebaseDatabaseTest::GetRemoteTimeInMilliseconds() {
firebase::database::DatabaseReference ref =
CreateWorkingPath().Child("timestamp");
WaitForCompletionAnyResult(
ref.SetValue(firebase::database::ServerTimestamp()),
"GetRemoteTime_SetValue");
firebase::Future<firebase::database::DataSnapshot> future = ref.GetValue();
WaitForCompletionAnyResult(future, "GetRemoteTime_GetValue");
int64_t timestamp = 0;
if (future.error() == 0 && future.result() &&
future.result()->value().is_int64()) {
timestamp = future.result()->value().int64_value();
}
if (timestamp > 0) {
LogDebug("Got server timestamp: %lld", timestamp);
LogDebug(" Local timestamp: %lld",
app_framework::GetCurrentTimeInMicroseconds() / 1000L);
return timestamp;
} else {
LogWarning("Couldn't get remote timestamp, using local time");
return app_framework::GetCurrentTimeInMicroseconds() / 1000L;
}
}

// Test cases below.
TEST_F(FirebaseDatabaseTest, TestInitializeAndTerminate) {
// Already tested via SetUp() and TearDown().
Expand Down Expand Up @@ -487,7 +461,7 @@ TEST_F(FirebaseDatabaseTest, TestSetAndGetSimpleValues) {
WaitForCompletion(f7, "GetLongDouble");

// Get the current time to compare to the Timestamp.
int64_t current_time_milliseconds = GetRemoteTimeInMilliseconds();
int64_t current_time_milliseconds = GetCurrentTimeInSecondsSinceEpoch() * 1000L;

EXPECT_EQ(f1.result()->value().AsString(), kSimpleString);
EXPECT_EQ(f2.result()->value().AsInt64(), kSimpleInt);
Expand Down Expand Up @@ -698,7 +672,7 @@ TEST_F(FirebaseDatabaseTest, TestUpdateChildren) {
WaitForCompletion(update_future, "UpdateChildren");
read_future = ref.Child(test_name).GetValue();
WaitForCompletion(read_future, "GetValue 2");
int64_t current_time_milliseconds = GetRemoteTimeInMilliseconds();
int64_t current_time_milliseconds = GetCurrentTimeInSecondsSinceEpoch() * 1000L;

EXPECT_THAT(
read_future.result()->value().map(),
Expand Down
28 changes: 1 addition & 27 deletions storage/integration_test/src/integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class FirebaseStorageTest : public FirebaseTest {
// Create a unique working folder and return a reference to it.
firebase::storage::StorageReference CreateFolder();

int64_t GetRemoteTimeInMilliseconds();

static firebase::App* shared_app_;
static firebase::auth::Auth* shared_auth_;

Expand Down Expand Up @@ -326,30 +324,6 @@ firebase::storage::StorageReference FirebaseStorageTest::CreateFolder() {
return storage_->GetReference(kRootNodeName).Child(saved_url_);
}

int64_t FirebaseStorageTest::GetRemoteTimeInMilliseconds() {
SignIn();

firebase::storage::StorageReference ref =
CreateFolder().Child("timestamp.txt");
firebase::Future<firebase::storage::Metadata> future =
RunWithRetry<firebase::storage::Metadata>(
[&]() { return ref.PutBytes("TS00", 4); });
WaitForCompletionAnyResult(future, "GetRemoteTime_PutBytes");
if (future.error() == 0 && future.result() != nullptr &&
future.result()->creation_time() > 0) {
int64_t timestamp = future.result()->creation_time();
WaitForCompletionAnyResult(RunWithRetry([&]() { return ref.Delete(); }),
"GetRemoteTime_Delete");
LogDebug("Got server timestamp: %lld", timestamp);
LogDebug(" Local timestamp: %lld",
app_framework::GetCurrentTimeInMicroseconds() / 1000L);
return timestamp;
} else {
LogWarning("Couldn't get remote timestamp, using local time");
return app_framework::GetCurrentTimeInMicroseconds() / 1000L;
}
}

// Test cases below.

TEST_F(FirebaseStorageTest, TestInitializeAndTerminate) {
Expand Down Expand Up @@ -523,7 +497,7 @@ TEST_F(FirebaseStorageTest, TestWriteAndReadFileWithCustomMetadata) {
ASSERT_NE(metadata, nullptr);

// Get the current time to compare to the Timestamp.
int64_t current_time_seconds = GetRemoteTimeInMilliseconds() / 1000L;
int64_t current_time_seconds = GetCurrentTimeInSecondsSinceEpoch();
int64_t updated_time_milliseconds = metadata->updated_time();
int64_t updated_time_seconds = updated_time_milliseconds / 1000L;
int64_t time_difference_seconds =
Expand Down

0 comments on commit c0afefa

Please sign in to comment.