1616
1717import * as nock from 'nock' ;
1818import * as assert from 'assert' ;
19- import { Resource } from '@opentelemetry/resources' ;
2019import { instanaAgentDetector } from '../src' ;
2120
2221describe ( '[UNIT] instanaAgentDetector' , ( ) => {
@@ -54,7 +53,8 @@ describe('[UNIT] instanaAgentDetector', () => {
5453 . put ( '/com.instana.plugin.nodejs.discovery' )
5554 . reply ( 200 , ( ) => mockedReply ) ;
5655
57- const resource : Resource = await instanaAgentDetector . detect ( ) ;
56+ const resource = instanaAgentDetector . detect ( ) ;
57+ await resource . waitForAsyncAttributes ?.( ) ;
5858
5959 scope . done ( ) ;
6060
@@ -80,7 +80,8 @@ describe('[UNIT] instanaAgentDetector', () => {
8080 . put ( '/com.instana.plugin.nodejs.discovery' )
8181 . reply ( 200 , ( ) => mockedReply ) ;
8282
83- const resource : Resource = await instanaAgentDetector . detect ( ) ;
83+ const resource = instanaAgentDetector . detect ( ) ;
84+ await resource . waitForAsyncAttributes ?.( ) ;
8485
8586 scope . done ( ) ;
8687
@@ -90,28 +91,24 @@ describe('[UNIT] instanaAgentDetector', () => {
9091 } ) ;
9192 } ) ;
9293
93- it ( 'agent throws error' , async ( ) => {
94- const expectedError = new Error ( 'Instana Agent returned status code 500' ) ;
94+ it ( 'agent returns empty resource if request error' , async ( ) => {
9595 const scope = nock ( 'http://localhost:42699' )
9696 . persist ( )
9797 . put ( '/com.instana.plugin.nodejs.discovery' )
9898 . reply ( 500 , ( ) => new Error ( ) ) ;
9999
100- try {
101- await instanaAgentDetector . detect ( ) ;
102- assert . ok ( false , 'Expected to throw' ) ;
103- } catch ( err ) {
104- assert . deepStrictEqual ( err , expectedError ) ;
105- }
100+ const resource = instanaAgentDetector . detect ( ) ;
101+ await resource . waitForAsyncAttributes ?.( ) ;
102+
103+ assert . deepStrictEqual ( resource . attributes , { } ) ;
106104
107105 scope . done ( ) ;
108106 } ) ;
109107
110- it ( 'agent timeout' , async ( ) => {
108+ it ( 'agent return empty resource if timeout' , async ( ) => {
111109 process . env . INSTANA_AGENT_PORT = '56002' ;
112110 process . env . INSTANA_AGENT_HOST = 'instanaagent' ;
113111 process . env . INSTANA_AGENT_TIMEOUT_MS = '200' ;
114- const expectedError = new Error ( 'Instana Agent request timed out.' ) ;
115112
116113 nock (
117114 `http://${ process . env . INSTANA_AGENT_HOST } :${ process . env . INSTANA_AGENT_PORT } `
@@ -121,28 +118,23 @@ describe('[UNIT] instanaAgentDetector', () => {
121118 . delay ( 500 )
122119 . reply ( 200 , { } ) ;
123120
124- try {
125- await instanaAgentDetector . detect ( ) ;
126- assert . ok ( false , 'Expected to throw' ) ;
127- } catch ( err ) {
128- console . log ( err ) ;
129- assert . deepStrictEqual ( err , expectedError ) ;
130- }
121+ const resource = instanaAgentDetector . detect ( ) ;
122+ await resource . waitForAsyncAttributes ?.( ) ;
123+
124+ assert . deepStrictEqual ( resource . attributes , { } ) ;
131125 } ) ;
132126 } ) ;
133127
134128 describe ( 'when agent is not running' , ( ) => {
135- it ( 'should not return agent resource' , async ( ) => {
129+ it ( 'should return empty resource' , async ( ) => {
136130 process . env . INSTANA_AGENT_PORT = '1111' ;
137131 process . env . INSTANA_AGENT_TIMEOUT_MS = '100' ;
138132 process . env . INSTANA_RETRY_TIMEOUT_MS = '100' ;
139133
140- try {
141- await instanaAgentDetector . detect ( ) ;
142- assert . ok ( false , 'Expected to throw' ) ;
143- } catch ( err : any ) {
144- assert . equal ( err . code , 'ECONNREFUSED' ) ;
145- }
134+ const resource = instanaAgentDetector . detect ( ) ;
135+ await resource . waitForAsyncAttributes ?.( ) ;
136+
137+ assert . deepStrictEqual ( resource . attributes , { } ) ;
146138 } ) ;
147139 } ) ;
148140} ) ;
0 commit comments