From 790ddb3a76702b451db648e160831cc5c2208e92 Mon Sep 17 00:00:00 2001 From: Isaries Date: Fri, 20 Nov 2026 14:25:11 +0800 Subject: [PATCH 1/2] fix(TeacherWebSocketService): Classroom monitor misses late-joining teams When a new workgroup joins the run, each period's classroom topic sends a newWorkgroupJoinedRun message and the classroom monitor calls ConfigService.retrieveConfig() to refresh its roster. retrieveConfig() returns a cold HTTP observable and nothing subscribed to it, so the request was never made, and the roster stayed as it was when the monitor loaded until the teacher reloaded or changed a team through manage students. The message now feeds a subject that requests the config at most once every two seconds. auditTime is used rather than debounceTime so the roster keeps refreshing while a class is still joining, instead of waiting for the joins to stop. A failed request is swallowed so later joins still refresh, and initialize() replaces any earlier pipeline instead of adding a second one. With the roster refreshing, the period selector counts and manage students update as teams join, and the other monitor views pick up the new team the next time they initialize instead of only after a reload. Anonymized discussion views compute pseudonyms from the roster, so this should land after the change that keeps those pseudonyms stable when the roster changes mid-session. --- .../services/teacherWebSocketService.spec.ts | 108 ++++++++++++++++++ .../wise5/services/teacherWebSocketService.ts | 23 +++- 2 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/app/services/teacherWebSocketService.spec.ts diff --git a/src/app/services/teacherWebSocketService.spec.ts b/src/app/services/teacherWebSocketService.spec.ts new file mode 100644 index 00000000000..f917c274be3 --- /dev/null +++ b/src/app/services/teacherWebSocketService.spec.ts @@ -0,0 +1,108 @@ +import { TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { MockProvider, MockProviders } from 'ng-mocks'; +import { defer, of, Subject, throwError } from 'rxjs'; +import { Message } from '@stomp/stompjs'; +import { RxStomp } from '@stomp/rx-stomp'; +import { AchievementService } from '../../assets/wise5/services/achievementService'; +import { ClassroomStatusService } from '../../assets/wise5/services/classroomStatusService'; +import { ConfigService } from '../../assets/wise5/services/configService'; +import { NotificationService } from '../../assets/wise5/services/notificationService'; +import { TeacherWebSocketService } from '../../assets/wise5/services/teacherWebSocketService'; + +let classroomMessage$: Subject; +let configRequestCount: number; +let configService: ConfigService; +let service: TeacherWebSocketService; + +describe('TeacherWebSocketService', () => { + beforeEach(() => { + classroomMessage$ = new Subject(); + TestBed.configureTestingModule({ + providers: [ + MockProviders(AchievementService, ClassroomStatusService, NotificationService), + MockProvider(ConfigService, { + getPeriods: () => [{ periodId: 1 }], + getRunId: () => 1 + }), + TeacherWebSocketService + ] + }); + configService = TestBed.inject(ConfigService); + service = TestBed.inject(TeacherWebSocketService); + spyOn(service, 'initializeStomp').and.callFake(() => { + service.rxStomp = { watch: () => classroomMessage$ } as unknown as RxStomp; + }); + spyOn(service, 'subscribeToTeacherTopic'); + spyOn(service, 'subscribeToTeacherWorkgroupTopic'); + configRequestCount = 0; + spyOn(configService, 'retrieveConfig').and.returnValue( + defer(() => { + configRequestCount++; + return of({}); + }) + ); + }); + newWorkgroupJoinedRun(); +}); + +function sendNewWorkgroupJoinedRun(): void { + classroomMessage$.next({ body: JSON.stringify({ type: 'newWorkgroupJoinedRun' }) } as Message); +} + +function newWorkgroupJoinedRun() { + describe('newWorkgroupJoinedRun message', () => { + it('should request the classroom monitor config', fakeAsync(() => { + service.initialize(); + sendNewWorkgroupJoinedRun(); + tick(2000); + expect(configService.retrieveConfig).toHaveBeenCalledWith('/api/config/classroomMonitor/1'); + expect(configRequestCount).toEqual(1); + })); + + it('should request the config once for a burst of joins', fakeAsync(() => { + service.initialize(); + for (let i = 0; i < 5; i++) { + sendNewWorkgroupJoinedRun(); + tick(100); + } + tick(10000); + expect(configRequestCount).toEqual(1); + })); + + it('should keep requesting the config while joins keep arriving', fakeAsync(() => { + service.initialize(); + for (let i = 0; i < 10; i++) { + sendNewWorkgroupJoinedRun(); + tick(1000); + } + expect(configRequestCount).toBeGreaterThanOrEqual(3); + })); + + it('should keep listening after a config request fails', fakeAsync(() => { + (configService.retrieveConfig as jasmine.Spy).and.returnValues( + defer(() => { + configRequestCount++; + return throwError(() => new Error('network error')); + }), + defer(() => { + configRequestCount++; + return of({}); + }) + ); + service.initialize(); + sendNewWorkgroupJoinedRun(); + tick(2000); + sendNewWorkgroupJoinedRun(); + tick(2000); + expect(configRequestCount).toEqual(2); + })); + + it('should keep one request pipeline when initialized again', fakeAsync(() => { + service.initialize(); + service.initialize(); + sendNewWorkgroupJoinedRun(); + tick(2000); + expect(configRequestCount).toEqual(1); + })); + }); +} diff --git a/src/assets/wise5/services/teacherWebSocketService.ts b/src/assets/wise5/services/teacherWebSocketService.ts index 33cd8eb9346..d4b88178cf1 100644 --- a/src/assets/wise5/services/teacherWebSocketService.ts +++ b/src/assets/wise5/services/teacherWebSocketService.ts @@ -4,7 +4,7 @@ import { Injectable } from '@angular/core'; import { ConfigService } from './configService'; import { ClassroomStatusService } from './classroomStatusService'; import { NotificationService } from './notificationService'; -import { Observable, Subject } from 'rxjs'; +import { auditTime, catchError, EMPTY, Observable, Subject, Subscription, switchMap } from 'rxjs'; import { AchievementService } from './achievementService'; import { RxStomp } from '@stomp/rx-stomp'; import { Message } from '@stomp/stompjs'; @@ -18,6 +18,8 @@ export class TeacherWebSocketService { public newAnnotationReceived$: Observable = this.newAnnotationReceivedSource.asObservable(); private newStudentWorkReceivedSource: Subject = new Subject(); public newStudentWorkReceived$: Observable = this.newStudentWorkReceivedSource.asObservable(); + private newWorkgroupJoinedRunSource: Subject = new Subject(); + private newWorkgroupJoinedRunSubscription: Subscription; constructor( private AchievementService: AchievementService, @@ -32,6 +34,7 @@ export class TeacherWebSocketService { this.subscribeToTeacherTopic(); this.subscribeToTeacherWorkgroupTopic(); this.subscribeToClassroomTopics(); + this.subscribeToNewWorkgroupJoinedRun(); } initializeStomp() { @@ -102,11 +105,23 @@ export class TeacherWebSocketService { .subscribe((message: Message) => { const body = JSON.parse(message.body); if (body.type === 'newWorkgroupJoinedRun') { - this.configService.retrieveConfig( - `/api/config/classroomMonitor/${this.configService.getRunId()}` - ); + this.newWorkgroupJoinedRunSource.next(); } }); } } + + private subscribeToNewWorkgroupJoinedRun(): void { + this.newWorkgroupJoinedRunSubscription?.unsubscribe(); + this.newWorkgroupJoinedRunSubscription = this.newWorkgroupJoinedRunSource + .pipe( + auditTime(2000), + switchMap(() => + this.configService + .retrieveConfig(`/api/config/classroomMonitor/${this.configService.getRunId()}`) + .pipe(catchError(() => EMPTY)) + ) + ) + .subscribe(); + } } From eaa9a67462d82d9e37a82593b51672d04025338d Mon Sep 17 00:00:00 2001 From: Isaries Date: Fri, 20 Nov 2026 15:11:09 +0800 Subject: [PATCH 2/2] fix(TeacherWebSocketService): Roster refresh never finishes during joins The roster refresh audits newWorkgroupJoinedRun messages and maps each audit tick to a config request with switchMap. While a class keeps joining, the audit window closes again every few seconds and switchMap cancels the request still in flight. If the config endpoint takes longer than the window to answer, no request completes until the joins stop. Use concatMap instead so each request runs to completion and the request for the latest joins still runs after the one before it. --- .../services/teacherWebSocketService.spec.ts | 21 +++++++++++++++++-- .../wise5/services/teacherWebSocketService.ts | 4 ++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/app/services/teacherWebSocketService.spec.ts b/src/app/services/teacherWebSocketService.spec.ts index f917c274be3..6fd538095fc 100644 --- a/src/app/services/teacherWebSocketService.spec.ts +++ b/src/app/services/teacherWebSocketService.spec.ts @@ -1,6 +1,6 @@ -import { TestBed, fakeAsync, tick } from '@angular/core/testing'; +import { TestBed, fakeAsync, flush, tick } from '@angular/core/testing'; import { MockProvider, MockProviders } from 'ng-mocks'; -import { defer, of, Subject, throwError } from 'rxjs'; +import { defer, delay, of, Subject, tap, throwError } from 'rxjs'; import { Message } from '@stomp/stompjs'; import { RxStomp } from '@stomp/rx-stomp'; import { AchievementService } from '../../assets/wise5/services/achievementService'; @@ -78,6 +78,23 @@ function newWorkgroupJoinedRun() { expect(configRequestCount).toBeGreaterThanOrEqual(3); })); + it('should finish slow config requests while joins keep arriving', fakeAsync(() => { + let answered = 0; + (configService.retrieveConfig as jasmine.Spy).and.returnValue( + of({}).pipe( + delay(3000), + tap(() => answered++) + ) + ); + service.initialize(); + for (let i = 0; i < 12; i++) { + sendNewWorkgroupJoinedRun(); + tick(1000); + } + expect(answered).toBeGreaterThanOrEqual(2); + flush(); + })); + it('should keep listening after a config request fails', fakeAsync(() => { (configService.retrieveConfig as jasmine.Spy).and.returnValues( defer(() => { diff --git a/src/assets/wise5/services/teacherWebSocketService.ts b/src/assets/wise5/services/teacherWebSocketService.ts index d4b88178cf1..b7b666d6b8d 100644 --- a/src/assets/wise5/services/teacherWebSocketService.ts +++ b/src/assets/wise5/services/teacherWebSocketService.ts @@ -4,7 +4,7 @@ import { Injectable } from '@angular/core'; import { ConfigService } from './configService'; import { ClassroomStatusService } from './classroomStatusService'; import { NotificationService } from './notificationService'; -import { auditTime, catchError, EMPTY, Observable, Subject, Subscription, switchMap } from 'rxjs'; +import { auditTime, catchError, concatMap, EMPTY, Observable, Subject, Subscription } from 'rxjs'; import { AchievementService } from './achievementService'; import { RxStomp } from '@stomp/rx-stomp'; import { Message } from '@stomp/stompjs'; @@ -116,7 +116,7 @@ export class TeacherWebSocketService { this.newWorkgroupJoinedRunSubscription = this.newWorkgroupJoinedRunSource .pipe( auditTime(2000), - switchMap(() => + concatMap(() => this.configService .retrieveConfig(`/api/config/classroomMonitor/${this.configService.getRunId()}`) .pipe(catchError(() => EMPTY))