Skip to content

Commit 67d9c5b

Browse files
jzdingclaude
andcommitted
CI: fix PushInitialEvent timeout and simplify crash recovery test
PushInitialEvent used Follow:true with scanner.Scan() but only checked the timeout after receiving a log line. If no lines arrived, it blocked indefinitely. Fix by passing context.WithTimeout to the log stream so scanner.Scan returns when the deadline expires. Also in the crash recovery test: - Remove redundant MonitorPodLogsRegex (verifyClockClassCurrentState uses PushInitialEvent which reads logs directly) - Rename verifyClockClassViaEventAPI to verifyClockClassCurrentState to reflect that it queries current state via fresh subscription with pushInitial=true, unlike verifyClockClassViaEvent which drains an existing long-lived subscription Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Jack Ding <jackding@gmail.com>
1 parent 1f2c2cc commit 67d9c5b

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

test/conformance/serial/ptp.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ var _ = Describe("["+strings.ToLower(DesiredMode.String())+"-serial]", Serial, f
13631363
}
13641364
})
13651365
time.Sleep(10 * time.Second)
1366+
event.InitPubSub()
13661367

13671368
By(fmt.Sprintf("Verifying initial clockClass is %s via metrics", expectedClockClassStr))
13681369
checkClockClassState(fullConfig, expectedClockClassStr)
@@ -1372,12 +1373,8 @@ var _ = Describe("["+strings.ToLower(DesiredMode.String())+"-serial]", Serial, f
13721373
By("Verifying initial gm.ClockClass is 6 via PMC")
13731374
checkClockClassViaPMC(fullConfig, strconv.Itoa(int(fbprotocol.ClockClass6)))
13741375

1375-
By(fmt.Sprintf("Setting up event monitoring and verifying initial clockClass is %s via Event API", expectedClockClassStr))
1376-
event.InitPubSub()
1377-
term, monErr := event.MonitorPodLogsRegex()
1378-
Expect(monErr).ToNot(HaveOccurred(), "could not start listening to events")
1379-
DeferCleanup(func() { stopMonitor(term) })
1380-
verifyClockClassViaEventAPI(int(expectedClockClass), 60*time.Second)
1376+
By(fmt.Sprintf("Verifying initial clockClass is %s via Event API", expectedClockClassStr))
1377+
verifyClockClassCurrentState(int(expectedClockClass), 60*time.Second)
13811378

13821379
By("Killing cloud-event-proxy process in sidecar container")
13831380
_, _, killErr := pods.ExecCommand(
@@ -1422,7 +1419,7 @@ var _ = Describe("["+strings.ToLower(DesiredMode.String())+"-serial]", Serial, f
14221419
checkClockClassViaPMC(fullConfig, strconv.Itoa(int(fbprotocol.ClockClass6)))
14231420

14241421
By(fmt.Sprintf("Verifying clockClass is %s via Event API after cloud-event-proxy restart", expectedClockClassStr))
1425-
verifyClockClassViaEventAPI(int(expectedClockClass), 90*time.Second)
1422+
verifyClockClassCurrentState(int(expectedClockClass), 90*time.Second)
14261423
})
14271424

14281425
Context("Event API version validation", func() {
@@ -3741,8 +3738,12 @@ func verifyClockClassViaEvent(evCtx bcEventContext, expectedClass int) {
37413738
verifyMetric(events[ptpEvent.PtpClockClassChange], float64(expectedClass))
37423739
}
37433740

3744-
// verifyClockClassViaEventAPI verifies clock class via Event Fast Notification API getCurrentState
3745-
func verifyClockClassViaEventAPI(expectedClockClass int, timeout time.Duration) {
3741+
// verifyClockClassCurrentState creates a fresh event subscription with pushInitial=true
3742+
// to query the current clock class state from the Event API's getCurrentState endpoint.
3743+
// Unlike verifyClockClassViaEvent (which drains an existing long-lived subscription),
3744+
// this function is safe to call after disruptions such as a cloud-event-proxy restart,
3745+
// where the previous subscription may be stale or disconnected.
3746+
func verifyClockClassCurrentState(expectedClockClass int, timeout time.Duration) {
37463747
const incomingEventsBuffer = 100
37473748
subs, cleanup := event.SubscribeToGMChangeEvents(incomingEventsBuffer, true, timeout)
37483749
defer cleanup()

test/pkg/event/event.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -523,39 +523,35 @@ func PushInitialEvent(eventType string, timeout time.Duration) (err error) {
523523
TailLines: &count,
524524
}
525525

526+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
527+
defer cancel()
528+
526529
podLogRequest := testclient.Client.CoreV1().Pods(namespace).GetLogs(podName, &podLogOptions)
527-
stream, err := podLogRequest.Stream(context.TODO())
530+
stream, err := podLogRequest.Stream(ctx)
528531
if err != nil {
529532
return fmt.Errorf("could not retrieve log in ns=%s pod=%s, err=%s", namespace, podName, err)
530533
}
531534
defer stream.Close()
532-
start := time.Now()
533-
for {
534-
scanner := bufio.NewScanner(stream)
535-
for scanner.Scan() {
536-
t := time.Now()
537-
elapsed := t.Sub(start)
538-
if elapsed > timeout {
539-
return fmt.Errorf("timedout PushInitialValue, waiting for log in ns=%s pod=%s, looking for = %s", namespace, podName, regex)
535+
536+
r := regexp.MustCompile(regex)
537+
scanner := bufio.NewScanner(stream)
538+
for scanner.Scan() {
539+
line := scanner.Text()
540+
logrus.Trace(line)
541+
542+
matches := r.FindAllStringSubmatch(line, -1)
543+
if len(matches) > 0 {
544+
aStoredEvent, eType, err := createStoredEvent([]byte(matches[0][1]))
545+
if err != nil {
546+
return err
540547
}
541-
line := scanner.Text()
542-
logrus.Trace(line)
543-
544-
r := regexp.MustCompile(regex)
545-
matches := r.FindAllStringSubmatch(line, -1)
546-
if len(matches) > 0 {
547-
aStoredEvent, eType, err := createStoredEvent([]byte(matches[0][1]))
548-
if err != nil {
549-
return err
550-
}
551-
if eType == eventType {
552-
PubSub.Publish(eType, aStoredEvent)
553-
return nil
554-
}
548+
if eType == eventType {
549+
PubSub.Publish(eType, aStoredEvent)
550+
return nil
555551
}
556552
}
557-
558553
}
554+
return fmt.Errorf("timed out PushInitialEvent, waiting for log in ns=%s pod=%s, looking for %s", namespace, podName, regex)
559555
}
560556
func createStoredEvent(data []byte) (aStoredEvent exports.StoredEvent, aType string, err error) {
561557
apiVersion := ptphelper.PtpEventEnabled()

0 commit comments

Comments
 (0)