Skip to content

Commit d83edf3

Browse files
committed
#2 replace redis mocks to testcontainers
1 parent faf7920 commit d83edf3

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

test/instrumentation/module/fix-redis.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test(`redis destination id`, async (t) => {
1818

1919
t.plan(6)
2020

21-
const trace = agent.createTraceObject()
21+
agent.createTraceObject()
2222
const redis = require('redis')
2323

2424
const client = redis.createClient(
@@ -68,7 +68,7 @@ test("ioredis destination id", async function (t) {
6868
container.getContainerIpAddress(),
6969
)
7070
redis.on("error", function (error) {
71-
console.error(error);
71+
console.error(error)
7272
})
7373

7474
const result = await redis.set("key", "value")
@@ -115,11 +115,11 @@ test(`Fix app crash without callback function https://github.com/pinpoint-apm/pi
115115

116116
t.plan(6)
117117

118-
const trace = agent.createTraceObject()
118+
agent.createTraceObject()
119119
const redis = require('redis')
120120

121121
const client = redis.createClient({
122-
host: container.getContainerIpAddress(),
122+
host: container.getContainerIpAddress(),
123123
port: container.getMappedPort(6379),
124124
db: 3,
125125
})

test/instrumentation/module/redis.test.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
const test = require('tape')
88
const axios = require('axios')
99

10-
const { log, fixture, util, enableDataSending } = require('../../test-helper')
10+
const { log } = require('../../test-helper')
1111

1212
const agent = require('../../support/agent-singleton-mock')
1313

1414
const express = require('express')
15-
const ioRedis = require('ioredis-mock')
16-
const Redis = require('redis-mock')
1715
const Koa = require('koa')
1816
const Router = require('koa-router')
1917
const koaBodyParser = require('koa-bodyparser')
20-
const rq = require('request')
18+
19+
const { GenericContainer } = require("testcontainers")
20+
21+
const ioRedis = require('ioredis-mock')
22+
const Redis = require('redis-mock')
2123

2224
const TEST_ENV = {
2325
host: 'localhost',
@@ -44,37 +46,37 @@ test(`${testName1} should Record the connections between express and redis.`, fu
4446
const PATH = `/${testName}`
4547

4648
app.use(express.json())
47-
app.use(function(req,res,next){
49+
app.use(function (req, res, next) {
4850
req.cache = client
4951
next()
5052
})
51-
app.post(PATH, function(req,res,next){
53+
app.post(PATH, function (req, res, next) {
5254
req.accepts('application/json')
5355
var key = req.body.name
5456
var value = JSON.stringify(req.body)
55-
req.cache.set(key,value,function(err,data){
56-
if(err){
57+
req.cache.set(key, value, function (err, data) {
58+
if (err) {
5759
console.log(err)
58-
res.send("error "+err)
60+
res.send("error " + err)
5961
return
6062
}
61-
req.cache.expire(key,10)
63+
req.cache.expire(key, 10)
6264
res.json(value)
6365
})
6466
})
65-
app.get(`${PATH}/:name`,function(req,res,next){
67+
app.get(`${PATH}/:name`, function (req, res, next) {
6668
var key = req.params.name
67-
req.cache.get(key, function(err,data){
68-
if(err){
69+
req.cache.get(key, function (err, data) {
70+
if (err) {
6971
console.log(err)
70-
res.send("error "+err)
72+
res.send("error " + err)
7173
return
7274
}
7375
var value = JSON.parse(data)
7476
res.json(value)
7577
})
7678
})
77-
app.use(function(req, res, next) {
79+
app.use(function (req, res, next) {
7880
var err = new Error('Not Found')
7981
err.status = 404
8082
next(err)
@@ -107,37 +109,37 @@ test(`${testName2} should Record the connections between express and ioredis.`,
107109
const PATH = `/${testName}`
108110

109111
app.use(express.json())
110-
app.use(function(req,res,next){
112+
app.use(function (req, res, next) {
111113
req.cache = redis
112114
next()
113115
})
114-
app.post(PATH, function(req,res,next){
116+
app.post(PATH, function (req, res, next) {
115117
req.accepts('application/json')
116118
var key = req.body.name
117119
var value = JSON.stringify(req.body)
118-
req.cache.set(key,value,function(err,data){
119-
if(err){
120+
req.cache.set(key, value, function (err, data) {
121+
if (err) {
120122
console.log(err)
121-
res.send("error "+err)
123+
res.send("error " + err)
122124
return
123125
}
124-
req.cache.expire(key,10)
126+
req.cache.expire(key, 10)
125127
res.json(value)
126128
})
127129
})
128-
app.get(`${PATH}/:name`,function(req,res,next){
130+
app.get(`${PATH}/:name`, function (req, res, next) {
129131
var key = req.params.name
130-
req.cache.get(key, function(err,data){
131-
if(err){
132+
req.cache.get(key, function (err, data) {
133+
if (err) {
132134
console.log(err)
133-
res.send("error "+err)
135+
res.send("error " + err)
134136
return
135137
}
136138
var value = JSON.parse(data)
137139
res.json(value)
138140
})
139141
})
140-
app.use(function(req, res, next) {
142+
app.use(function (req, res, next) {
141143
var err = new Error('Not Found')
142144
err.status = 404
143145
next(err)
@@ -171,13 +173,13 @@ test(`${testName3} should Record the connections between koa and redis.`, functi
171173

172174
const PATH = `/${testName}`
173175
app.use(koaBodyParser())
174-
router.post(PATH, async function(ctx, next) {
176+
router.post(PATH, async function (ctx, next) {
175177
console.log(ctx.request.body)
176178
const key = ctx.request.body.name
177179
const value = JSON.stringify(ctx.request.body)
178180

179-
client.set(key, value, function(err, data) {
180-
if(err){
181+
client.set(key, value, function (err, data) {
182+
if (err) {
181183
console.log(err)
182184
ctx.body = `error :: ${err}`
183185
return
@@ -216,7 +218,7 @@ test(`${testName3} should Record the connections between koa and redis.`, functi
216218
const testName4 = 'koa-ioredis'
217219
test(`${testName4} should Record the connections between koa and ioredis.`, function (t) {
218220
agent.bindHttp()
219-
221+
220222
const testName = testName4
221223

222224
t.plan(2)
@@ -227,7 +229,7 @@ test(`${testName4} should Record the connections between koa and ioredis.`, func
227229

228230
const PATH = `/${testName}`
229231
app.use(koaBodyParser())
230-
router.post(PATH, async function(ctx, next) {
232+
router.post(PATH, async function (ctx, next) {
231233
const key = ctx.request.body.name
232234
const value = JSON.stringify(ctx.request.body)
233235
await redis.set(key, value)

0 commit comments

Comments
 (0)