|
| 1 | +/* |
| 2 | + * Copyright 2020 New Relic Corporation. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +#include "tlib_php.h" |
| 6 | +#include "tlib_datastore.h" |
| 7 | + |
| 8 | +#include "php_agent.h" |
| 9 | +#include "php_memcached.h" |
| 10 | +#include "util_system.h" |
| 11 | + |
| 12 | +tlib_parallel_info_t parallel_info |
| 13 | + = {.suggested_nthreads = -1, .state_size = 0}; |
| 14 | + |
| 15 | +static char* system_host_name; |
| 16 | + |
| 17 | +static void test_create_datastore_instance(void) { |
| 18 | + assert_datastore_instance_equals_destroy( |
| 19 | + "named socket", |
| 20 | + &((nr_datastore_instance_t){ |
| 21 | + .host = system_host_name, |
| 22 | + .database_name = "unknown", |
| 23 | + .port_path_or_id = "/tmp/memcached.sock", |
| 24 | + }), |
| 25 | + nr_php_memcached_create_datastore_instance("/tmp/memcached.sock", 0)); |
| 26 | + |
| 27 | + assert_datastore_instance_equals_destroy( |
| 28 | + "empty socket", |
| 29 | + &((nr_datastore_instance_t){ |
| 30 | + .host = system_host_name, |
| 31 | + .database_name = "unknown", |
| 32 | + .port_path_or_id = "unknown", |
| 33 | + }), |
| 34 | + nr_php_memcached_create_datastore_instance("", 0)); |
| 35 | + |
| 36 | + assert_datastore_instance_equals_destroy( |
| 37 | + "empty host", |
| 38 | + &((nr_datastore_instance_t){ |
| 39 | + .host = system_host_name, |
| 40 | + .database_name = "unknown", |
| 41 | + .port_path_or_id = "unknown", |
| 42 | + }), |
| 43 | + nr_php_memcached_create_datastore_instance(NULL, 0)); |
| 44 | + |
| 45 | + assert_datastore_instance_equals_destroy( |
| 46 | + "host.name socket", |
| 47 | + &((nr_datastore_instance_t){ |
| 48 | + .host = "host.name", |
| 49 | + .database_name = "unknown", |
| 50 | + .port_path_or_id = "11211", |
| 51 | + }), |
| 52 | + nr_php_memcached_create_datastore_instance("host.name", 11211)); |
| 53 | + |
| 54 | + assert_datastore_instance_equals_destroy( |
| 55 | + "host and port", |
| 56 | + &((nr_datastore_instance_t){ |
| 57 | + .host = "unknown", |
| 58 | + .database_name = "unknown", |
| 59 | + .port_path_or_id = "6379", |
| 60 | + }), |
| 61 | + nr_php_memcached_create_datastore_instance("", 6379)); |
| 62 | + |
| 63 | + assert_datastore_instance_equals_destroy( |
| 64 | + "NULL socket", |
| 65 | + &((nr_datastore_instance_t){ |
| 66 | + .host = "unknown", |
| 67 | + .database_name = "unknown", |
| 68 | + .port_path_or_id = "11211", |
| 69 | + }), |
| 70 | + nr_php_memcached_create_datastore_instance(NULL, 11211)); |
| 71 | +} |
| 72 | + |
| 73 | +static void test_create_instance_metric(void) { |
| 74 | + nrtxn_t* txn; |
| 75 | + nrmetric_t* metric; |
| 76 | + char* metric_str; |
| 77 | + tlib_php_engine_create(""); |
| 78 | + tlib_php_request_start(); |
| 79 | + txn = NRPRG(txn); |
| 80 | + |
| 81 | + nr_php_memcached_create_instance_metric("host", 11211); |
| 82 | + metric = nrm_find(txn->unscoped_metrics, "Datastore/instance/Memcached/host/11211"); |
| 83 | + tlib_pass_if_not_null("metric found", metric); |
| 84 | + |
| 85 | + nr_php_memcached_create_instance_metric("", 11211); |
| 86 | + metric = nrm_find(txn->unscoped_metrics, "Datastore/instance/Memcached/unknown/11211"); |
| 87 | + tlib_pass_if_not_null("metric found", metric); |
| 88 | + |
| 89 | + nr_php_memcached_create_instance_metric(NULL, 7); |
| 90 | + metric = nrm_find(txn->unscoped_metrics, "Datastore/instance/Memcached/unknown/7"); |
| 91 | + tlib_pass_if_not_null("metric found", metric); |
| 92 | + |
| 93 | + nr_php_memcached_create_instance_metric("path/to/sock", 0); |
| 94 | + metric_str = nr_formatf("Datastore/instance/Memcached/%s/path/to/sock", system_host_name); |
| 95 | + metric = nrm_find(txn->unscoped_metrics, metric_str); |
| 96 | + nr_free(metric_str); |
| 97 | + tlib_pass_if_not_null("metric found", metric); |
| 98 | + |
| 99 | + nr_php_memcached_create_instance_metric("", 0); |
| 100 | + metric_str = nr_formatf("Datastore/instance/Memcached/%s/unknown", system_host_name); |
| 101 | + metric = nrm_find(txn->unscoped_metrics, metric_str); |
| 102 | + nr_free(metric_str); |
| 103 | + tlib_pass_if_not_null("metric found", metric); |
| 104 | + |
| 105 | + // restart the transaction because the next metric is the same as a previous metric |
| 106 | + tlib_php_request_end(); |
| 107 | + tlib_php_request_start(); |
| 108 | + txn = NRPRG(txn); |
| 109 | + |
| 110 | + nr_php_memcached_create_instance_metric(NULL, 0); |
| 111 | + metric_str = nr_formatf("Datastore/instance/Memcached/%s/unknown", system_host_name); |
| 112 | + metric = nrm_find(txn->unscoped_metrics, metric_str); |
| 113 | + nr_free(metric_str); |
| 114 | + tlib_pass_if_not_null("metric found", metric); |
| 115 | + |
| 116 | + tlib_php_request_end(); |
| 117 | + tlib_php_engine_destroy(); |
| 118 | +} |
| 119 | + |
| 120 | +void test_main(void* p NRUNUSED) { |
| 121 | + system_host_name = nr_system_get_hostname(); |
| 122 | + |
| 123 | + test_create_datastore_instance(); |
| 124 | + test_create_instance_metric(); |
| 125 | + |
| 126 | + nr_free(system_host_name); |
| 127 | +} |
0 commit comments