2424namespace ElasticApmTests \ComponentTests ;
2525
2626use Elastic \Apm \Impl \Config \OptionNames ;
27+ use ElasticApmTests \ComponentTests \Util \AmbientContextForTests ;
2728use ElasticApmTests \ComponentTests \Util \AppCodeHostParams ;
2829use ElasticApmTests \ComponentTests \Util \AppCodeRequestParams ;
2930use ElasticApmTests \ComponentTests \Util \AppCodeTarget ;
3031use ElasticApmTests \ComponentTests \Util \ComponentTestCaseBase ;
31- use ElasticApmTests \ComponentTests \Util \ExpectedEventCounts ;
3232use ElasticApmTests \Util \AssertMessageStack ;
3333use ElasticApmTests \Util \DataProviderForTestBuilder ;
3434use ElasticApmTests \Util \MixedMap ;
3939 */
4040final class AgentEnabledConfigComponentTest extends ComponentTestCaseBase
4141{
42- private const ENABLED_CONFIG_SEEN_BY_TEST = 'enabled_config_seen_by_test ' ;
43-
44- public static function appCodeForTestWhenAgentIsDisabledItShouldNotSendAnyData (MixedMap $ appCodeArgs ): void
42+ public static function appCodeForTestAgentEnabledConfig (MixedMap $ appCodeArgs ): void
4543 {
46- $ enabledConfigSeenByTest = $ appCodeArgs ->getBool (self :: ENABLED_CONFIG_SEEN_BY_TEST );
44+ $ agentEnabledExpected = $ appCodeArgs ->getBool (OptionNames:: ENABLED );
4745
4846 /**
4947 * elastic_apm_* functions are provided by the elastic_apm extension
5048 *
5149 * @noinspection PhpFullyQualifiedNameUsageInspection, PhpUndefinedFunctionInspection
5250 * @phpstan-ignore-next-line
5351 */
54- $ enabledConfigSeenByNativePart = \elastic_apm_is_enabled ();
55- self ::assertSame ($ enabledConfigSeenByTest , $ enabledConfigSeenByNativePart );
52+ $ agentEnabledAsSeenByNativePart = \elastic_apm_is_enabled ();
53+ self ::assertSame ($ agentEnabledExpected , $ agentEnabledAsSeenByNativePart );
5654 }
5755
5856 /**
5957 * @return iterable<string, array{MixedMap}>
6058 */
61- public static function dataProviderForTestWhenAgentIsDisabledItShouldNotSendAnyData (): iterable
59+ public static function dataProviderForTestAgentEnabledConfig (): iterable
6260 {
6361 $ result = (new DataProviderForTestBuilder ())
64- ->addKeyedDimensionOnlyFirstValueCombinable (OptionNames::ENABLED , [null , true . false ])
62+ ->addKeyedDimensionAllValuesCombinable (OptionNames::ENABLED , [null , true , false ])
6563 ->build ();
6664
6765 return DataProviderForTestBuilder::convertEachDataSetToMixedMap ($ result );
6866 }
6967
7068 /**
71- * @dataProvider dataProviderForTestWhenAgentIsDisabledItShouldNotSendAnyData
69+ * @dataProvider dataProviderForTestAgentEnabledConfig
7270 */
73- public function testWhenAgentIsDisabledItShouldNotSendAnyData (MixedMap $ testArgs ): void
71+ public function testAgentEnabledConfig (MixedMap $ testArgs ): void
7472 {
7573 AssertMessageStack::newScope (/* out */ $ dbgCtx , AssertMessageStack::funcArgs ());
7674
@@ -81,26 +79,32 @@ function (AppCodeHostParams $appCodeParams) use ($testArgs): void {
8179 }
8280 );
8381
84- $ enabledConfigSeenByTest = $ appCodeHost ->appCodeHostParams ->getEffectiveAgentConfig ()->enabled ();
85- $ dbgCtx ->add (compact ('enabledConfigSeenByTest ' ));
86- $ logger = self ::getLogger (__NAMESPACE__ , __CLASS__ , __FILE__ );
87- ($ loggerProxy = $ logger ->ifInfoLevelEnabled (__LINE__ , __FUNCTION__ )) && $ loggerProxy ->log ('' , ['enabledConfigSeenByTest ' => $ enabledConfigSeenByTest ]);
82+ $ agentEnabledTestArg = $ testArgs ->getNullableBool (OptionNames::ENABLED );
83+ $ agentEnabledActual = $ appCodeHost ->appCodeHostParams ->getEffectiveAgentConfig ()->enabled ();
84+ if (($ agentEnabledExpected = $ agentEnabledTestArg ?? AmbientContextForTests::testConfig ()->agentEnabledConfigDefault ) !== null ) {
85+ self ::assertSame ($ agentEnabledExpected , $ agentEnabledActual );
86+ }
8887
8988 $ appCodeHost ->sendRequest (
90- AppCodeTarget::asRouted ([__CLASS__ , 'appCodeForTestWhenAgentIsDisabledItShouldNotSendAnyData ' ]),
91- function (AppCodeRequestParams $ appCodeRequestParams ) use ($ enabledConfigSeenByTest ): void {
92- $ appCodeRequestParams ->setAppCodeArgs ([self :: ENABLED_CONFIG_SEEN_BY_TEST => $ enabledConfigSeenByTest ]);
89+ AppCodeTarget::asRouted ([__CLASS__ , 'appCodeForTestAgentEnabledConfig ' ]),
90+ function (AppCodeRequestParams $ appCodeRequestParams ) use ($ agentEnabledActual ): void {
91+ $ appCodeRequestParams ->setAppCodeArgs ([OptionNames:: ENABLED => $ agentEnabledActual ]);
9392 }
9493 );
9594
96- if ($ enabledConfigSeenByTest ) {
95+ if ($ agentEnabledActual ) {
9796 $ dataFromAgent = $ this ->waitForOneEmptyTransaction ($ testCaseHandle );
9897 self ::assertCount (1 , $ dataFromAgent ->idToTransaction );
9998 self ::assertCount (1 , $ dataFromAgent ->metadatas );
10099 } else {
101- sleep (/* seconds */ 5 );
102- $ dataFromAgent = $ testCaseHandle ->waitForDataFromAgent ((new ExpectedEventCounts ())->metadatas (0 )->transactions (0 ), /* shouldValidate */ false );
100+ $ logger = self ::getLoggerStatic (__NAMESPACE__ , __CLASS__ , __FILE__ );
101+ $ waitTimeSeconds = 5 ;
102+ ($ loggerProxy = $ logger ->ifInfoLevelEnabled (__LINE__ , __FUNCTION__ ))
103+ && $ loggerProxy ->log ('Sleeping ' . $ waitTimeSeconds . ' seconds to give agent enough time to send data (which it should not since it is disabled)... ' );
104+ sleep ($ waitTimeSeconds );
105+ $ dataFromAgent = $ testCaseHandle ->getDataFromAgentWithoutWaiting ();
103106 self ::assertCount (0 , $ dataFromAgent ->idToTransaction );
107+ self ::assertCount (0 , $ dataFromAgent ->metadatas );
104108 }
105109 self ::assertCount (0 , $ dataFromAgent ->idToSpan );
106110 self ::assertCount (0 , $ dataFromAgent ->idToError );
0 commit comments