Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return wrong context in promise chain #28

Open
tunv-pnc opened this issue Dec 27, 2018 · 4 comments
Open

Return wrong context in promise chain #28

tunv-pnc opened this issue Dec 27, 2018 · 4 comments

Comments

@tunv-pnc
Copy link

I'm using express-http-context to store request-id(generate each time new request arrived automatically) and get request-id anytime, anywhere I want.
In normal case its working fine but if I get context in promise chain it always return context of the first request. Please help me how to always get context of current request.

Thanks a lot!

@salmgazer
Copy link

@tunv-pnc , did you find a solution to this? kindly share if you did.

@rohitsud
Copy link

It is possible that you are using a middleware that is not hooked with a CLS compatible promise library. This is a major drawback in using CLS; you need to know all your dependencies that are called in the promise chain and middlewares support CLS.

@halu886
Copy link

halu886 commented Jul 2, 2019

@tunv-pnc
hello,
I try to do a unit test by supertest,but not find the situation.
Can you help to check it? :)

it('middleware set request-id concurrency', function (done) {
	// ARRANGE
	const app = express();

	app.use(httpContext.middleware);

	const mw = (req,res,next)=>{
		httpContext.set("request-id",req.header("request-id"));
		next();
	}
	app.use(mw);
	app.get('/test', async (req, res) => {
		const delay = new Number(req.query.delay);
		await new Promise(resolve => setTimeout(resolve, delay)).then(()=>{
				res.status(200).json({
					value: httpContext.get("request-id")
				});
		});
	});

	const sut = supertest(app);

	const request1 = '1';
	const request2 = '2';

	// ACT
	sut.get('/test').set("request-id",request1).query({ delay: 100}).end((err, res) => {
		// ASSERT
		
		assert.equal(res.body.value, request1);
		done();
	});
	
	sut.get('/test').set("request-id",request2).query({ delay: 50}).end((err, res) => {
		// ASSERT
		assert.equal(res.body.value, request2);
	});

});

image

@halu886
Copy link

halu886 commented Jul 2, 2019

@skonves Hi,Can I make pull request this unit test to this repository?:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants