Test python implementation of get_remaining_time_in_millis

This commit is contained in:
Tommy Brunn 2017-08-05 19:47:46 +02:00
parent 35102ee3e6
commit 5a61f6c756
No known key found for this signature in database
GPG Key ID: C362861DEE8B6B6E
4 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,11 @@
from time import sleep
def withRemainingTime(event, context):
start = context.get_remaining_time_in_millis()
sleep(0.001)
stop = context.get_remaining_time_in_millis()
return {
"start": start,
"stop": stop
}

View File

@ -452,4 +452,35 @@ describe('AwsInvokeLocal', () => {
expect(serverless.cli.consoleLog.lastCall.args[0]).to.contain('"errorMessage": "failed"');
});
});
describe('#invokeLocalPython', () => {
beforeEach(() => {
awsInvokeLocal.options = {
functionObj: {
name: '',
},
};
serverless.cli = new CLI(serverless);
sinon.stub(serverless.cli, 'consoleLog');
});
afterEach(() => {
serverless.cli.consoleLog.restore();
});
describe('context.remainingTimeInMillis', () => {
it('should become lower over time', () => {
awsInvokeLocal.serverless.config.servicePath = __dirname;
return awsInvokeLocal.invokeLocalPython(
'python2.7',
'fixture/handler',
'withRemainingTime').then(() => {
const remainingTimes = JSON.parse(serverless.cli.consoleLog.lastCall.args[0]);
expect(remainingTimes.start).to.be.above(remainingTimes.stop);
});
});
});
});
});

View File

@ -12,7 +12,7 @@ class FakeLambdaContext(object):
self.timeout = timeout
def get_remaining_time_in_millis(self):
return max((self.timeout * 1000) - (time() - self.created), 0)
return int(max((self.timeout * 1000) - (int(round(time() * 1000)) - int(round(self.created * 1000))), 0))
@property
def function_name(self):