Merge "Add test for AStatsSocket_close()" into rvc-dev

This commit is contained in:
Jeffrey Huang 2020-04-29 18:46:26 +00:00 committed by Android (Google) Code Review
commit 053ade4320
6 changed files with 59 additions and 1 deletions

View file

@ -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",

View file

@ -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
}

View file

@ -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;

View file

@ -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;

View file

@ -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

View file

@ -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 <gtest/gtest.h>
#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());
}