Code review comment: I solved it by setting it to `numSplits + 2`.

Code under review:
public void testMetrics() throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        env.getConfig().setAutoWatermarkInterval(1L);

        int numSplits = 2;
        int numRecordsPerSplit = 10;
        MockBaseSource source =
                new MockBaseSource(numSplits, numRecordsPerSplit, Boundedness.BOUNDED);

        long baseTime = System.currentTimeMillis() - EVENTTIME_LAG;
        WatermarkStrategy<Integer> strategy =
                WatermarkStrategy.<Integer>forBoundedOutOfOrderness(
                                Duration.ofMillis(WATERMARK_LAG))
                        .withTimestampAssigner(new LaggingTimestampAssigner(baseTime));

        
        
        SharedReference<CyclicBarrier> beforeBarrier =
                sharedObjects.add(new CyclicBarrier(numSplits + 1));
        SharedReference<CyclicBarrier> afterBarrier =
                sharedObjects.add(new CyclicBarrier(numSplits + 1));
        DataStream<Integer> stream =
                env.fromSource(source, strategy, "TestingSource")
                        .map(
                                i -> {
                                    if (i % numRecordsPerSplit == 3
                                            || i % numRecordsPerSplit == 9) {
                                        beforeBarrier.get().await();
                                        afterBarrier.get().await();
                                    }
                                    return i;
                                });
        stream.addSink(new DiscardingSink<>());
        JobClient jobClient = env.executeAsync();

        beforeBarrier.get().await();
        assertSourceMetrics(4, numRecordsPerSplit, env.getParallelism());
        afterBarrier.get().await();

        beforeBarrier.get().await();
        assertSourceMetrics(10, numRecordsPerSplit, env.getParallelism());
        afterBarrier.get().await();

        jobClient.getJobExecutionResult().get();
    }

Line commented on: env.setParallelism(4);

Revised code:
public void testMetrics() throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        int numSplits = Math.max(1, env.getParallelism() - 2);
        env.getConfig().setAutoWatermarkInterval(1L);

        int numRecordsPerSplit = 10;
        MockBaseSource source =
                new MockBaseSource(numSplits, numRecordsPerSplit, Boundedness.BOUNDED);

        long baseTime = System.currentTimeMillis() - EVENTTIME_LAG;
        WatermarkStrategy<Integer> strategy =
                WatermarkStrategy.forGenerator(
                                context -> new EagerBoundedOutOfOrdernessWatermarks())
                        .withTimestampAssigner(new LaggingTimestampAssigner(baseTime));

        
        
        SharedReference<CyclicBarrier> beforeBarrier =
                sharedObjects.add(new CyclicBarrier(numSplits + 1));
        SharedReference<CyclicBarrier> afterBarrier =
                sharedObjects.add(new CyclicBarrier(numSplits + 1));
        int stopAtRecord1 = 3;
        int stopAtRecord2 = numRecordsPerSplit - 1;
        DataStream<Integer> stream =
                env.fromSource(source, strategy, "TestingSource")
                        .map(
                                i -> {
                                    if (i % numRecordsPerSplit == stopAtRecord1
                                            || i % numRecordsPerSplit == stopAtRecord2) {
                                        beforeBarrier.get().await();
                                        afterBarrier.get().await();
                                    }
                                    return i;
                                });
        stream.addSink(new DiscardingSink<>());
        JobClient jobClient = env.executeAsync();

        beforeBarrier.get().await();
        assertSourceMetrics(stopAtRecord1 + 1, numRecordsPerSplit, env.getParallelism(), numSplits);
        afterBarrier.get().await();

        beforeBarrier.get().await();
        assertSourceMetrics(stopAtRecord2 + 1, numRecordsPerSplit, env.getParallelism(), numSplits);
        afterBarrier.get().await();

        jobClient.getJobExecutionResult().get();
    }