From 25dde6d415e97a504cb6c18386192a79790ca93c Mon Sep 17 00:00:00 2001
From: Doug Evans <dje@google.com>
Date: Mon, 13 May 2019 19:34:45 +0000
Subject: [PATCH] [trace-test-utils] Move test fixture here for use outside
 zircon

Change-Id: I960886bae1a318c96153c0ce69b90d0a55c6ee38
---
 zircon/system/ulib/trace-provider/BUILD.gn    |  8 ++-
 zircon/system/ulib/trace-test-utils/BUILD.gn  | 17 +++++-
 .../ulib/trace-test-utils/compare_records.cpp |  4 --
 .../trace-test-utils}/fixture.cpp             | 31 +++++------
 .../include/trace-test-utils}/fixture.h       | 48 +----------------
 zircon/system/utest/trace/BUILD.gn            |  4 +-
 zircon/system/utest/trace/engine_tests.cpp    |  7 +--
 .../system/utest/trace/event_tests_common.h   |  4 +-
 zircon/system/utest/trace/fixture_macros.h    | 53 +++++++++++++++++++
 zircon/system/utest/trace/record_tests.cpp    |  5 +-
 10 files changed, 102 insertions(+), 79 deletions(-)
 rename zircon/system/{utest/trace => ulib/trace-test-utils}/fixture.cpp (94%)
 rename zircon/system/{utest/trace => ulib/trace-test-utils/include/trace-test-utils}/fixture.h (61%)
 create mode 100644 zircon/system/utest/trace/fixture_macros.h

diff --git a/zircon/system/ulib/trace-provider/BUILD.gn b/zircon/system/ulib/trace-provider/BUILD.gn
index e37b44c3d76..d7ba7b2c16f 100644
--- a/zircon/system/ulib/trace-provider/BUILD.gn
+++ b/zircon/system/ulib/trace-provider/BUILD.gn
@@ -125,11 +125,15 @@ library("trace-provider-with-static-engine") {
 # Normal apps are not expected to use this.
 library("trace-handler") {
   sdk = "static"
-  sdk_headers = []
+  sdk_headers = [
+    "trace-provider/handler.h",
+  ]
   sources = []
   deps = [
     ":handler",
-    "$zx/system/ulib/trace-engine",
+    # It is up to the client to choose which engine (shared, static),
+    # just reference the headers here.
+    "$zx/system/ulib/trace-engine:headers",
   ]
 }
 
diff --git a/zircon/system/ulib/trace-test-utils/BUILD.gn b/zircon/system/ulib/trace-test-utils/BUILD.gn
index 8e09898bbd2..d58bea0245e 100644
--- a/zircon/system/ulib/trace-test-utils/BUILD.gn
+++ b/zircon/system/ulib/trace-test-utils/BUILD.gn
@@ -2,25 +2,40 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+# Note: The appearance of "sdk" does not mean this will or is intended to be
+# in the sdk. It's just the way we export from zircon for use elsewhere.
 library("trace-test-utils") {
   sdk = "source"
   sdk_headers = [
     "trace-test-utils/compare_records.h",
+    "trace-test-utils/fixture.h",
     "trace-test-utils/read_records.h",
     "trace-test-utils/squelch.h",
   ]
   testonly = true
   sources = [
     "compare_records.cpp",
+    "fixture.cpp",
     "read_records.cpp",
     "squelch.cpp",
   ]
   configs += [ "$zx/public/gn/config:visibility_hidden" ]
+  public_deps = [
+    # <trace-test-utils/fixture.h> needs these
+    "$zx/system/ulib/async-loop:headers",
+    "$zx/system/ulib/fbl:headers",
+    "$zx/system/ulib/trace-engine:headers",
+    "$zx/system/ulib/trace-reader:headers",
+  ]
   deps = [
+    # The kind of engine to use is up to the client, so we don't provide any
+    # trace-engine dependency here.
+    "$zx/system/ulib/async-loop",
+    "$zx/system/ulib/async-loop:async-loop-cpp",
     "$zx/system/ulib/fbl",
     "$zx/system/ulib/trace-provider:trace-handler",
     "$zx/system/ulib/trace-reader",
-    "$zx/system/ulib/unittest",
     "$zx/system/ulib/zircon",
+    "$zx/system/ulib/zx",
   ]
 }
diff --git a/zircon/system/ulib/trace-test-utils/compare_records.cpp b/zircon/system/ulib/trace-test-utils/compare_records.cpp
index 78f6ab7e522..19361f7e030 100644
--- a/zircon/system/ulib/trace-test-utils/compare_records.cpp
+++ b/zircon/system/ulib/trace-test-utils/compare_records.cpp
@@ -15,7 +15,6 @@
 #include <fbl/string_buffer.h>
 #include <trace-reader/reader.h>
 #include <trace-reader/reader_internal.h>
-#include <unittest/unittest.h>
 
 #include "trace-test-utils/read_records.h"
 #include "trace-test-utils/squelch.h"
@@ -45,9 +44,6 @@ bool CompareRecords(const fbl::Vector<trace::Record>& records,
         fbl::String from_str = record.ToString();
         fbl::String to_str = squelcher->Squelch(from_str.c_str());
 
-        UNITTEST_TRACEF(2, "Squelch from: %s\n", from_str.c_str());
-        UNITTEST_TRACEF(2, "Squelch to:   %s\n", to_str.c_str());
-
         buf.Append(to_str);
         buf.Append('\n');
         ++num_recs;
diff --git a/zircon/system/utest/trace/fixture.cpp b/zircon/system/ulib/trace-test-utils/fixture.cpp
similarity index 94%
rename from zircon/system/utest/trace/fixture.cpp
rename to zircon/system/ulib/trace-test-utils/fixture.cpp
index 409c89cc0ba..fbbd19e0335 100644
--- a/zircon/system/utest/trace/fixture.cpp
+++ b/zircon/system/ulib/trace-test-utils/fixture.cpp
@@ -2,12 +2,13 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "fixture.h"
+#include "trace-test-utils/fixture.h"
 
 #include <regex.h>
 #include <stdint.h>
 #include <string.h>
 #include <sys/types.h>
+#include <utility>
 
 #include <zircon/assert.h>
 
@@ -23,9 +24,6 @@
 #include <trace-reader/reader_internal.h>
 #include <trace-test-utils/compare_records.h>
 #include <trace-test-utils/read_records.h>
-#include <unittest/unittest.h>
-
-#include <utility>
 
 namespace {
 
@@ -289,32 +287,31 @@ void fixture_reset_buffer_full_notification() {
     g_fixture->ResetBufferFullNotification();
 }
 
+bool fixture_read_records(fbl::Vector<trace::Record>* out_records) {
+    return g_fixture->ReadRecords(out_records);
+}
+
 bool fixture_compare_raw_records(const fbl::Vector<trace::Record>& records,
                                  size_t start_record, size_t max_num_records,
                                  const char* expected) {
-    BEGIN_HELPER;
-
-    ASSERT_TRUE(trace_testing::CompareRecords(records, start_record,
-                                              max_num_records, expected));
-
-    END_HELPER;
+    return trace_testing::CompareRecords(records, start_record,
+                                         max_num_records, expected);
 }
 
 bool fixture_compare_n_records(size_t max_num_records, const char* expected,
                                fbl::Vector<trace::Record>* out_records,
                                size_t* out_leading_to_skip) {
     ZX_DEBUG_ASSERT(g_fixture);
-    BEGIN_HELPER;
 
     g_fixture->StopTracing(false);
 
-    ASSERT_TRUE(g_fixture->ReadRecords(out_records), "read error");
-
-    ASSERT_TRUE(trace_testing::ComparePartialBuffer(*out_records,
-                                                    max_num_records, expected,
-                                                    out_leading_to_skip));
+    if (!fixture_read_records(out_records)) {
+        return false;
+    }
 
-    END_HELPER;
+    return trace_testing::ComparePartialBuffer(*out_records,
+                                               max_num_records, expected,
+                                               out_leading_to_skip);
 }
 
 bool fixture_compare_records(const char* expected) {
diff --git a/zircon/system/utest/trace/fixture.h b/zircon/system/ulib/trace-test-utils/include/trace-test-utils/fixture.h
similarity index 61%
rename from zircon/system/utest/trace/fixture.h
rename to zircon/system/ulib/trace-test-utils/include/trace-test-utils/fixture.h
index eee0ab2d177..ceebb54d84a 100644
--- a/zircon/system/utest/trace/fixture.h
+++ b/zircon/system/ulib/trace-test-utils/include/trace-test-utils/fixture.h
@@ -21,7 +21,6 @@
 
 #include <lib/async-loop/loop.h>
 #include <trace-engine/types.h>
-#include <unittest/unittest.h>
 #include <zircon/compiler.h>
 
 // Specifies whether the trace engine async loop uses the same thread as the
@@ -35,6 +34,8 @@ typedef enum {
 
 #ifdef __cplusplus
 
+bool fixture_read_records(fbl::Vector<trace::Record>* out_records);
+
 bool fixture_compare_raw_records(const fbl::Vector<trace::Record>& records,
                                  size_t start_record, size_t max_num_records,
                                  const char* expected);
@@ -83,49 +84,4 @@ static inline void fixture_scope_cleanup(bool* scope) {
     fixture_tear_down();
 }
 
-#define DEFAULT_BUFFER_SIZE_BYTES (1024u * 1024u)
-
-// This isn't a do-while because of the cleanup.
-#define BEGIN_TRACE_TEST_ETC(attach_to_thread, mode, buffer_size) \
-    BEGIN_TEST;                                                   \
-    __attribute__((cleanup(fixture_scope_cleanup))) bool __scope; \
-    (void)__scope;                                                \
-    fixture_set_up((attach_to_thread), (mode), (buffer_size))
-
-#define BEGIN_TRACE_TEST \
-    BEGIN_TRACE_TEST_ETC(kNoAttachToThread, TRACE_BUFFERING_MODE_ONESHOT, \
-                         DEFAULT_BUFFER_SIZE_BYTES)
-
-#define END_TRACE_TEST \
-    END_TEST;
-
-#ifndef NTRACE
-
-#ifdef __cplusplus
-#define ASSERT_RECORDS(expected_c, expected_cpp) \
-    ASSERT_TRUE(fixture_compare_records(expected_c expected_cpp), \
-                "record mismatch")
-#define ASSERT_N_RECORDS(max_num_recs, expected_c, expected_cpp, \
-                         records, skip_count)                      \
-    ASSERT_TRUE(fixture_compare_n_records((max_num_recs),          \
-                expected_c expected_cpp, (records), (skip_count)), \
-                "record mismatch")
-#else
-#define ASSERT_RECORDS(expected_c, expected_cpp) \
-    ASSERT_TRUE(fixture_compare_records(expected_c), "record mismatch")
-#endif // __cplusplus
-
-#else // NTRACE
-
-#define ASSERT_RECORDS(expected_c, expected_cpp) \
-    ASSERT_TRUE(fixture_compare_records(""), "record mismatch")
-#ifdef __cplusplus
-#define ASSERT_N_RECORDS(max_num_recs, expected_c, expected_cpp, \
-                         records, skip_count)                    \
-    ASSERT_TRUE(fixture_compare_records((max_num_recs), "",      \
-                (records), (skip_count)), "record mismatch")
-#endif
-
-#endif // NTRACE
-
 __END_CDECLS
diff --git a/zircon/system/utest/trace/BUILD.gn b/zircon/system/utest/trace/BUILD.gn
index b55d0596860..73527271df4 100644
--- a/zircon/system/utest/trace/BUILD.gn
+++ b/zircon/system/utest/trace/BUILD.gn
@@ -13,7 +13,6 @@ source_set("common") {
     "event_tests_ntrace.c",
     "event_tests_ntrace.cpp",
     "fields_tests.cpp",
-    "fixture.cpp",
     "no_optimization.c",
     "record_tests.cpp",
   ]
@@ -25,7 +24,8 @@ source_set("common") {
     "$zx/system/ulib/async-loop:async-loop-cpp",
     "$zx/system/ulib/fbl",
     "$zx/system/ulib/trace",
-    "$zx/system/ulib/trace-provider:trace-handler",
+    # The engine is chosen by the binary, we just need engine headers here.
+    "$zx/system/ulib/trace-engine:headers",
     "$zx/system/ulib/trace-reader",
     "$zx/system/ulib/trace-test-utils",
     "$zx/system/ulib/unittest",
diff --git a/zircon/system/utest/trace/engine_tests.cpp b/zircon/system/utest/trace/engine_tests.cpp
index 85e649dc7e1..6d18f7aa083 100644
--- a/zircon/system/utest/trace/engine_tests.cpp
+++ b/zircon/system/utest/trace/engine_tests.cpp
@@ -2,8 +2,6 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "fixture.h"
-
 #include <threads.h>
 
 #include <atomic>
@@ -15,9 +13,12 @@
 #include <fbl/vector.h>
 #include <lib/zx/event.h>
 #include <trace/event.h>
-#include <trace-provider/handler.h>
+#include <trace-engine/handler.h>
+#include <trace-test-utils/fixture.h>
 #include <trace-test-utils/squelch.h>
 
+#include "fixture_macros.h"
+
 namespace {
 
 using trace_site_atomic_state_t = std::atomic<trace_site_state_t>;
diff --git a/zircon/system/utest/trace/event_tests_common.h b/zircon/system/utest/trace/event_tests_common.h
index 12ae3c01eb7..24f6150bfd4 100644
--- a/zircon/system/utest/trace/event_tests_common.h
+++ b/zircon/system/utest/trace/event_tests_common.h
@@ -13,7 +13,7 @@
 
 #include <trace/event.h>
 #include <trace/event_args.h>
-
+#include <trace-test-utils/fixture.h>
 #include <unittest/unittest.h>
 #include <zircon/syscalls.h>
 
@@ -22,7 +22,7 @@
 #include <fbl/string_piece.h>
 #endif // __cplusplus
 
-#include "fixture.h"
+#include "fixture_macros.h"
 
 #define I32_ARGS1 "k1", TA_INT32(1)
 #define I32_ARGS2 "k1", TA_INT32(1), "k2", TA_INT32(2)
diff --git a/zircon/system/utest/trace/fixture_macros.h b/zircon/system/utest/trace/fixture_macros.h
new file mode 100644
index 00000000000..37cda60e0ca
--- /dev/null
+++ b/zircon/system/utest/trace/fixture_macros.h
@@ -0,0 +1,53 @@
+// Copyright 2017 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#pragma once
+
+#include <trace-test-utils/fixture.h>
+#include <unittest/unittest.h>
+
+#define DEFAULT_BUFFER_SIZE_BYTES (1024u * 1024u)
+
+// This isn't a do-while because of the cleanup.
+#define BEGIN_TRACE_TEST_ETC(attach_to_thread, mode, buffer_size) \
+    BEGIN_TEST;                                                   \
+    __attribute__((cleanup(fixture_scope_cleanup))) bool __scope; \
+    (void)__scope;                                                \
+    fixture_set_up((attach_to_thread), (mode), (buffer_size))
+
+#define BEGIN_TRACE_TEST \
+    BEGIN_TRACE_TEST_ETC(kNoAttachToThread, TRACE_BUFFERING_MODE_ONESHOT, \
+                         DEFAULT_BUFFER_SIZE_BYTES)
+
+#define END_TRACE_TEST \
+    END_TEST;
+
+#ifndef NTRACE
+
+#ifdef __cplusplus
+#define ASSERT_RECORDS(expected_c, expected_cpp) \
+    ASSERT_TRUE(fixture_compare_records(expected_c expected_cpp), \
+                "record mismatch")
+#define ASSERT_N_RECORDS(max_num_recs, expected_c, expected_cpp, \
+                         records, skip_count)                      \
+    ASSERT_TRUE(fixture_compare_n_records((max_num_recs),          \
+                expected_c expected_cpp, (records), (skip_count)), \
+                "record mismatch")
+#else
+#define ASSERT_RECORDS(expected_c, expected_cpp) \
+    ASSERT_TRUE(fixture_compare_records(expected_c), "record mismatch")
+#endif // __cplusplus
+
+#else // NTRACE
+
+#define ASSERT_RECORDS(expected_c, expected_cpp) \
+    ASSERT_TRUE(fixture_compare_records(""), "record mismatch")
+#ifdef __cplusplus
+#define ASSERT_N_RECORDS(max_num_recs, expected_c, expected_cpp, \
+                         records, skip_count)                    \
+    ASSERT_TRUE(fixture_compare_records((max_num_recs), "",      \
+                (records), (skip_count)), "record mismatch")
+#endif
+
+#endif // NTRACE
diff --git a/zircon/system/utest/trace/record_tests.cpp b/zircon/system/utest/trace/record_tests.cpp
index 9ce181c86fb..0c01c192660 100644
--- a/zircon/system/utest/trace/record_tests.cpp
+++ b/zircon/system/utest/trace/record_tests.cpp
@@ -2,14 +2,15 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "fixture.h"
-
 #include <fbl/algorithm.h>
 #include <fbl/string_printf.h>
 #include <trace/event.h>
 #include <trace-engine/instrumentation.h>
+#include <trace-test-utils/fixture.h>
 #include <zircon/syscalls.h>
 
+#include "fixture_macros.h"
+
 namespace {
 
 static bool blob_test(void) {
-- 
GitLab