diff --git a/libstats/socket/Android.bp b/libstats/socket/Android.bp index f386243c4..e40a432be 100644 --- a/libstats/socket/Android.bp +++ b/libstats/socket/Android.bp @@ -109,7 +109,10 @@ cc_benchmark { cc_test { name: "libstatssocket_test", - srcs: ["tests/stats_event_test.cpp"], + srcs: [ + "tests/stats_event_test.cpp", + "tests/stats_writer_test.cpp", + ], cflags: [ "-Wall", "-Werror", diff --git a/libstats/socket/include/stats_buffer_writer.h b/libstats/socket/include/stats_buffer_writer.h index de4a5e21d..5b41f0e04 100644 --- a/libstats/socket/include/stats_buffer_writer.h +++ b/libstats/socket/include/stats_buffer_writer.h @@ -23,6 +23,7 @@ extern "C" { #endif // __CPLUSPLUS void stats_log_close(); +int stats_log_is_closed(); int write_buffer_to_statsd(void* buffer, size_t size, uint32_t atomId); #ifdef __cplusplus } diff --git a/libstats/socket/stats_buffer_writer.c b/libstats/socket/stats_buffer_writer.c index c5c591d78..74acb20dc 100644 --- a/libstats/socket/stats_buffer_writer.c +++ b/libstats/socket/stats_buffer_writer.c @@ -43,6 +43,10 @@ void stats_log_close() { statsd_writer_init_unlock(); } +int stats_log_is_closed() { + return statsdLoggerWrite.isClosed && (*statsdLoggerWrite.isClosed)(); +} + int write_buffer_to_statsd(void* buffer, size_t size, uint32_t atomId) { int ret = 1; diff --git a/libstats/socket/statsd_writer.c b/libstats/socket/statsd_writer.c index 04d3b4644..73b7a7e71 100644 --- a/libstats/socket/statsd_writer.c +++ b/libstats/socket/statsd_writer.c @@ -62,6 +62,7 @@ static int statsdOpen(); static void statsdClose(); static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr); static void statsdNoteDrop(); +static int statsdIsClosed(); struct android_log_transport_write statsdLoggerWrite = { .name = "statsd", @@ -71,6 +72,7 @@ struct android_log_transport_write statsdLoggerWrite = { .close = statsdClose, .write = statsdWrite, .noteDrop = statsdNoteDrop, + .isClosed = statsdIsClosed, }; /* log_init_lock assumed */ @@ -153,6 +155,13 @@ static void statsdNoteDrop(int error, int tag) { atomic_exchange_explicit(&atom_tag, tag, memory_order_relaxed); } +static int statsdIsClosed() { + if (atomic_load(&statsdLoggerWrite.sock) < 0) { + return 1; + } + return 0; +} + static int statsdWrite(struct timespec* ts, struct iovec* vec, size_t nr) { ssize_t ret; int sock; diff --git a/libstats/socket/statsd_writer.h b/libstats/socket/statsd_writer.h index fe2d37cbc..562bda5df 100644 --- a/libstats/socket/statsd_writer.h +++ b/libstats/socket/statsd_writer.h @@ -40,6 +40,8 @@ struct android_log_transport_write { int (*write)(struct timespec* ts, struct iovec* vec, size_t nr); /* note one log drop */ void (*noteDrop)(int error, int tag); + /* checks if the socket is closed */ + int (*isClosed)(); }; #endif // ANDROID_STATS_LOG_STATS_WRITER_H diff --git a/libstats/socket/tests/stats_writer_test.cpp b/libstats/socket/tests/stats_writer_test.cpp new file mode 100644 index 000000000..47f3517b8 --- /dev/null +++ b/libstats/socket/tests/stats_writer_test.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * 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 +#include "stats_buffer_writer.h" +#include "stats_event.h" +#include "stats_socket.h" + +TEST(StatsWriterTest, TestSocketClose) { + EXPECT_TRUE(stats_log_is_closed()); + + AStatsEvent* event = AStatsEvent_obtain(); + AStatsEvent_setAtomId(event, 100); + AStatsEvent_writeInt32(event, 5); + AStatsEvent_build(event); + int successResult = AStatsEvent_write(event); + AStatsEvent_release(event); + + // In the case of a successful write, we return the number of bytes written. + EXPECT_GT(successResult, 0); + EXPECT_FALSE(stats_log_is_closed()); + + AStatsSocket_close(); + + EXPECT_TRUE(stats_log_is_closed()); +} \ No newline at end of file