Skip to content

Commit 60eccce

Browse files
committed
test: add caching for GitHub App ID retrieval in rate limit metric tests
1 parent ab07871 commit 60eccce

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lambdas/functions/control-plane/src/github/rate-limit.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createSingleMetric } from '@aws-github-runner/aws-powertools-util';
33
import { MetricUnit } from '@aws-lambda-powertools/metrics';
44
import { metricGitHubAppRateLimit } from './rate-limit';
55
import { describe, it, expect, beforeEach, vi } from 'vitest';
6+
import { getParameter } from '@aws-github-runner/aws-ssm-util';
67

78
process.env.PARAMETER_GITHUB_APP_ID_NAME = 'test';
89
vi.mock('@aws-github-runner/aws-ssm-util', async () => {
@@ -78,4 +79,27 @@ describe('metricGitHubAppRateLimit', () => {
7879

7980
expect(createSingleMetric).not.toHaveBeenCalled();
8081
});
82+
83+
it('should cache GitHub App ID and only call getParameter once', async () => {
84+
// Reset modules to clear the appIdPromise cache
85+
vi.resetModules();
86+
const { metricGitHubAppRateLimit: freshMetricFunction } = await import('./rate-limit');
87+
88+
process.env.ENABLE_METRIC_GITHUB_APP_RATE_LIMIT = 'true';
89+
const headers: ResponseHeaders = {
90+
'x-ratelimit-remaining': '10',
91+
'x-ratelimit-limit': '60',
92+
};
93+
94+
const mockGetParameter = vi.mocked(getParameter);
95+
mockGetParameter.mockClear();
96+
97+
await freshMetricFunction(headers);
98+
await freshMetricFunction(headers);
99+
await freshMetricFunction(headers);
100+
101+
// getParameter should only be called once due to caching
102+
expect(mockGetParameter).toHaveBeenCalledTimes(1);
103+
expect(mockGetParameter).toHaveBeenCalledWith(process.env.PARAMETER_GITHUB_APP_ID_NAME);
104+
});
81105
});

0 commit comments

Comments
 (0)