Skip to content

Commit

Permalink
[#193] gRPC DataSource specific method Test Fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Aug 5, 2024
1 parent 642d799 commit a453d02
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions test/client/grpc-unary-rpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ const MethodDescriptorBuilder = require('../../lib/context/method-descriptor-bui
const CallArgumentsBuilder = require('../../lib/client/call-arguments-builder')
const config = require('../../lib/config')

let callCount = 0
let afterCount = 0
// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/v5.0.0/examples/src/grpcjs/client.ts
const requestAgentInfo = function requestAgentInfo(call, callback) {
const succeedOnRetryAttempt = call.metadata.get('succeed-on-retry-attempt')
const previousAttempts = call.metadata.get('grpc-previous-rpc-attempts')
const delayTime = call.metadata.get('delay-time')

if (succeedOnRetryAttempt.length === 0 || (previousAttempts.length === 0 && previousAttempts[0] === succeedOnRetryAttempt[0])) {
console.debug(`succeed-on-retry-attempt: ${succeedOnRetryAttempt[0]}, grpc-previous-rpc-attempts: ${previousAttempts[0]}`)
if (succeedOnRetryAttempt.length === 0 || (previousAttempts.length > 0 && previousAttempts[0] === succeedOnRetryAttempt[0])) {
const result = new spanMessages.PResult()
result.setSuccess(true)
result.setMessage(`succeed-on-retry-attempt: ${succeedOnRetryAttempt[0]}, grpc-previous-rpc-attempts: ${previousAttempts[0]}, delay-time: ${delayTime[0]}`)
result.setMessage(`succeed-on-retry-attempt: ${succeedOnRetryAttempt[0]}, grpc-previous-rpc-attempts: ${previousAttempts[0]}`)
callback(null, result)
// _.delay(() => {
// }, parseInt(delayTime[0] || 0))
} else {
const statusCode = call.metadata.get('respond-with-status')
const code = statusCode[0] ? Number.parseInt(statusCode[0]) : grpc.status.UNKNOWN
Expand Down Expand Up @@ -63,7 +63,9 @@ function before(port, serviceConfig) {
return dataSenderFactory.create(actualConfig, agentInfo())
}

function beforeOne(port, one, serviceConfig) {
function beforeSpecificOne(port, one, serviceConfig) {
callCount = 0
afterCount = 0
config.clear()
const actualConfig = config.getConfig({ 'grpc.service_config': serviceConfig })
actualConfig.collectorIp = 'localhost'
Expand All @@ -78,13 +80,24 @@ function beforeOne(port, one, serviceConfig) {
actualConfig.collectorSpanPort,
agentInfo(),
actualConfig
)
)
}

function afterOne(t) {
afterCount++
if (callCount === afterCount) {
t.end()
}
}

class GrpcAgentInfoOnlyDataSource extends GrpcDataSender {
constructor(collectorIp, collectorTcpPort, collectorStatPort, collectorSpanPort, agentInfo, config) {
super(collectorIp, collectorTcpPort, collectorStatPort, collectorSpanPort, agentInfo, config)
}
sendAgentInfo(agentInfo, callArguments) {
callCount++
super.sendAgentInfo(agentInfo, callArguments)
}
initializeMetadataClients() { }
initializeSpanStream() { }
initializeStatStream() { }
Expand All @@ -108,7 +121,6 @@ test.skip('AgentInfo with retries enabled but not configured', (t) => {
t.fail(error)
}
t.true(response.getSuccess(), '1st PResult.success is true')
t.equal(response.getMessage(), 'succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined', '1st PResult.message is "succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined"')
}).build()
dataSender.dataSender.sendAgentInfo(agentInfo(), callArguments)

Expand All @@ -123,7 +135,6 @@ test.skip('AgentInfo with retries enabled but not configured', (t) => {
t.fail(error)
}
t.true(response.getSuccess(), '3st PResult.success is true')
t.equal(response.getMessage(), 'succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined', '3st PResult.message is "succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined"')
t.end()
}).build()
dataSender.dataSender.sendAgentInfo(agentInfo(), callArguments)
Expand All @@ -144,11 +155,11 @@ test('AgentInfo with retries enabled and configured', (t) => {

let dataSender
server.bindAsync('localhost:0', grpc.ServerCredentials.createInsecure(), (error, port) => {
dataSender = beforeOne(port, GrpcAgentInfoOnlyDataSource, {
dataSender = beforeSpecificOne(port, GrpcAgentInfoOnlyDataSource, {
methodConfig: [
{
name: [
{ service: 'Agent' }
{ service: 'v1.Agent' }
],
retryPolicy: {
maxAttempts: 3,
Expand All @@ -166,12 +177,15 @@ test('AgentInfo with retries enabled and configured', (t) => {
t.fail(error)
}
t.true(response.getSuccess(), '1st PResult.success is true')
t.equal(response.getMessage(), 'succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined', '1st PResult.message is "succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined"')
t.equal(response.getMessage(), 'succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined', '1st PResult.message is "succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined"')
afterOne(t)
}).build()
dataSender.sendAgentInfo(agentInfo(), callArguments)

callArguments = new CallArgumentsBuilder(function (error, response) {
// t.true(response.getSuccess(), '2nd PResult.success is true')
t.true(response.getSuccess(), '2nd PResult.success is true')
t.equal(response.getMessage(), 'succeed-on-retry-attempt: 2, grpc-previous-rpc-attempts: 2', '2nd PResult.message is "succeed-on-retry-attempt: 2, grpc-previous-rpc-attempts: 2"')
afterOne(t)
}).setMetadata('succeed-on-retry-attempt', '2')
.setMetadata('respond-with-status', '14')
.build()
Expand All @@ -182,8 +196,8 @@ test('AgentInfo with retries enabled and configured', (t) => {
t.fail(error)
}
t.true(response.getSuccess(), '3st PResult.success is true')
t.equal(response.getMessage(), 'succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined', '3st PResult.message is "succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined, delay-time: undefined"')
t.end()
t.equal(response.getMessage(), 'succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined', '3st PResult.message is "succeed-on-retry-attempt: undefined, grpc-previous-rpc-attempts: undefined"')
afterOne(t)
}).build()
dataSender.sendAgentInfo(agentInfo(), callArguments)
})
Expand Down

0 comments on commit a453d02

Please sign in to comment.