|
| 1 | +import {test, expect} from '@playwright/test'; |
| 2 | + |
| 3 | +const BASE_URL = (process.env.BASE_URL || 'http://localhost').replace(/\/+$/, ''); |
| 4 | +const SUCCESS_PATH = /\/redis-lottery-queue\/success\/?$/; |
| 5 | + |
| 6 | +test.describe.serial('redis-lottery-queue - Redis Lottery Queue', () => { |
| 7 | + test.beforeEach(async ({request}) => { |
| 8 | + // Clear all Redis keys for this example |
| 9 | + const response = await request.get(`${BASE_URL}/reset`); |
| 10 | + console.log('Reset response:', await response.json()); |
| 11 | + }); |
| 12 | + |
| 13 | + test('should display initial state correctly', async ({page}) => { |
| 14 | + await page.goto(`${BASE_URL}/redis-lottery-queue/`); |
| 15 | + |
| 16 | + await expect(page.locator('h1')).toHaveText('Lottery Queue'); |
| 17 | + await expect(page.locator('#go')).toBeEnabled(); |
| 18 | + await expect(page.locator('#reset')).toBeEnabled(); |
| 19 | + await expect(page.locator('#status')).toBeEmpty(); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should allow one user in immediately', async ({page}) => { |
| 23 | + await page.goto(`${BASE_URL}/redis-lottery-queue/`); |
| 24 | + |
| 25 | + await page.locator('#go').click(); |
| 26 | + |
| 27 | + // Should show you got in immediately |
| 28 | + await expect(page.locator('#status')).toContainText('You got in immediately! Redirecting...'); |
| 29 | + |
| 30 | + // Wait for redirect and check page title |
| 31 | + await expect(page).toHaveURL(SUCCESS_PATH); |
| 32 | + await expect(page.locator('h1')).toHaveText('You Made It!'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should allow one user in and make one user wait', async ({browser}) => { |
| 36 | + const context1 = await browser.newContext(); |
| 37 | + const context2 = await browser.newContext(); |
| 38 | + const page1 = await context1.newPage(); |
| 39 | + const page2 = await context2.newPage(); |
| 40 | + |
| 41 | + try { |
| 42 | + // First user gets in |
| 43 | + await page1.goto(`${BASE_URL}/redis-lottery-queue/`); |
| 44 | + await page1.locator('#go').click(); |
| 45 | + await expect(page1.locator('#status')).toContainText('You got in immediately!'); |
| 46 | + await expect(page1).toHaveURL(SUCCESS_PATH); |
| 47 | + |
| 48 | + // Second user has to wait |
| 49 | + await page2.goto(`${BASE_URL}/redis-lottery-queue/`); |
| 50 | + await page2.locator('#go').click(); |
| 51 | + await expect(page2.locator('#status')).toContainText('Waiting in lottery queue...'); |
| 52 | + |
| 53 | + // Release explicitly from user 1's session instead of relying on the 5s client timer. |
| 54 | + const releaseResponse = await context1.request.post(`${BASE_URL}/redis-lottery-queue/release`); |
| 55 | + expect(releaseResponse.ok()).toBeTruthy(); |
| 56 | + |
| 57 | + // Second user redirected to the success page |
| 58 | + await expect(page2.locator('#status')).toContainText('Your turn! Redirecting...', {timeout: 10000}); |
| 59 | + await expect(page2).toHaveURL(SUCCESS_PATH, {timeout: 5000}); |
| 60 | + await expect(page2.locator('h1')).toHaveText('You Made It!'); |
| 61 | + } finally { |
| 62 | + await context1.close(); |
| 63 | + await context2.close(); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + /*test('second user should be blocked when lock is held', async ({browser}) => { |
| 68 | + // Create two browser contexts (simulating two users) |
| 69 | + const context1 = await browser.newContext(); |
| 70 | + const context2 = await browser.newContext(); |
| 71 | + const page1 = await context1.newPage(); |
| 72 | + const page2 = await context2.newPage(); |
| 73 | +
|
| 74 | + try { |
| 75 | + // First user starts action |
| 76 | + await page1.goto('/global-lock/'); |
| 77 | + await page1.locator('#go').click(); |
| 78 | + await expect(page1.locator('#status')).toContainText('Processing'); |
| 79 | +
|
| 80 | + // Second user tries to start - should be instantly blocked |
| 81 | + await page2.goto('/global-lock/'); |
| 82 | + await page2.locator('#go').click(); |
| 83 | + await expect(page2.locator('#status')).toContainText('Already processing'); |
| 84 | + await expect(page2.locator('#status')).toHaveClass(/error/); |
| 85 | +
|
| 86 | + // Wait for first user to complete |
| 87 | + await expect(page1.locator('#status')).toContainText('Done', {timeout: 5000}); |
| 88 | +
|
| 89 | + // Second user can now start |
| 90 | + await page2.locator('#go').click(); |
| 91 | + await expect(page2.locator('#status')).toContainText('Processing'); |
| 92 | + } finally { |
| 93 | + await context1.close(); |
| 94 | + await context2.close(); |
| 95 | + } |
| 96 | + });*!/*/ |
| 97 | +}); |
0 commit comments