You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: stream_event_batcher_test.go
    path of file: ./repos/cockroach/pkg/ccl/streamingccl/streamproducer
    
    the code of the file until where you have to start completion: // Copyright 2023 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
//     https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt

package streamproducer

import (
	"fmt"
	"testing"

	"github.com/cockroachdb/cockroach/pkg/ccl/streamingccl/replicationtestutils"
	"github.com/cockroachdb/cockroach/pkg/keys"
	"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
	"github.com/cockroachdb/cockroach/pkg/repstream/streampb"
	"github.com/cockroachdb/cockroach/pkg/roachpb"
	"github.com/cockroachdb/cockroach/pkg/util/hlc"
	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
	"
    
func TestBatchSpanConfigs(t *testing.T) {
	defer leaktest.AfterTest(t)()
	defer log.Scope(t).Close(t)

	rng, seed := randutil.NewPseudoRand()
	t.Logf("Replication helper seed %d", seed)

	seb := makeStreamEventBatcher()
	codec := keys.MakeSQLCodec(roachpb.MustMakeTenantID(2))
	bufferedEvents := make([]streampb.StreamedSpanConfigEntry, 0)

	tableSpan := func(tableID uint32) roachpb.Span {
		syntheticTableSpanPrefix := codec.TablePrefix(tableID)
		return roachpb.Span{Key: syntheticTableSpanPrefix, EndKey: syntheticTableSpanPrefix.PrefixEnd()}
	}

	makeEntry := func(tableSpan roachpb.Span, ttl int, timestamp int64) streampb.StreamedSpanConfigEntry {
		record := replicationtestutils.MakeSpanConfigRecord(t, tableSpan, ttl)
		return streampb.StreamedSpanConfigEntry{
			SpanConfig: roachpb.SpanConfigEntry{
				Target: record.GetTarget().ToProto(),
				Config: record.GetConfig(),
			},
			Timestamp: hlc.Timestamp{WallTime: timestamp},
		}
	}

	events := func(events ...streampb.StreamedSpanConfigEntry) []streampb.StreamedSpanConfigEntry {
		return events
	}

	t1Span := tableSpan(1)
	t2Span := tableSpan(2)

	previousBatchIdx := 0

	for idx, toAdd := range []struct {
		updates          []streampb.StreamedSpanConfigEntry
		frontier         int64
		expected         []streampb.StreamedSpanConfigEntry
		shouldFlushAfter bool
	}{
		{
			// Both spans should buffer
			updates:  events(makeEntry(t1Span, 3, 1), makeEntry(t1Span, 4, 2)),
			frontier: 2,
			expected: events(makeEntry(t1Span, 3, 1), makeEntry(t1Span, 4, 2)),
		},
		{
			// An event at a lower timestamp should not get flushed
			updates:  events(makeEntry(t1Span, 3, 1)),
			frontier: 2,
			expected: []streampb.StreamedSpanConfigEntry{},
		},
		{
			// An empty update should not screw things up.
			updates:  []streampb.StreamedSpanConfigEntry{},
			frontier: 2,
			expected: []streampb.StreamedSpanConfigEntry{},
		},
		{
			// An event at the frontier should not get flushed
			updates:  events(makeEntry(t1Span, 4, 2)),
			frontier: 2,
			expected: []streampb.StreamedSpanConfigEntry{},
		},
		{
			// Only one of the duplicate events should flush
			updates:  events(makeEntry(t1Span, 3, 3), makeEntry(t1Span, 3, 3)),
			frontier: 3,
			expected: events(makeEntry(t1Span, 3, 3)),
		},
		{
			// Only one of the duplicate events should flush, even if there's an event in between the duplicate events.
			updates:  events(makeEntry(t1Span, 3, 4), makeEntry(t2Span, 4, 4), makeEntry(t1Span, 3, 4)),
			frontier: 4,
			expected: events(makeEntry(t1Span, 3, 4), makeEntry(t2Span, 4, 4)),
		},
		// Duplicate events from previous addition get elided.
		{
			updates:  events(makeEntry(t1Span, 3, 4), makeEntry(t2Span, 4, 4), makeEntry(t1Span, 3, 4), makeEntry(t2Span, 3, 5)),
			frontier: 5,
			expected: []streampb.StreamedSpanConfigEntry{makeEntry(t2Span, 3, 5)},
		},
	} {
		bufferedEvents = append(bufferedEvents, toAdd.updates...)
		seb.addSpanConfigs(bufferedEvents, hlc.Timestamp{WallTime: toAdd.frontier})
		require.Equal(t, toAdd.expected, seb.batch.SpanConfigs[previousBatchIdx:], fmt.Sprintf("failed on step %d (indexed by 0)", idx))
		bufferedEvents = bufferedEvents[:0]
		if rng.Intn(2) == 0 {
			// Flush half of the time.
			t.Logf("Flushing batcher on step %d (indexed by 0)", idx)
			seb.reset()
		}
		previousBatchIdx = len(seb.batch.SpanConfigs)
	}
}