<commit_before>package service

import (
	"errors"
	"sync/atomic"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func testSubscriptionWatchError(t *testing.T) {
	var (
		assert        = assert.New(t)
		expectedError = errors.New("expected")
		registrar     = new(mockRegistrar)

		subscription = Subscription{
			Registrar: registrar,
			Listener: func([]string) {
				assert.Fail("The listener should not have been called")
			},
		}
	)

	registrar.On("Watch").Once().Return(nil, expectedError)

	assert.Equal(expectedError, subscription.Run())
	assert.Equal(ErrorNotRunning, subscription.Cancel())

	registrar.AssertExpectations(t)
}

func testSubscriptionListenerPanic(t *testing.T) {
	var (
		assert        = assert.New(t)
		expectedError = errors.New("expected")

		watch     = new(mockWatch)
		registrar = new(mockRegistrar)

		expectedEndpoints = []string{"expected endpoint"}
		listenerCalled    = make(chan struct{})

		subscription = Subscription{
			Registrar: registrar,
			Listener: func(endpoints []string) {
				defer close(listenerCalled)
				assert.Equal(expectedEndpoints, endpoints)
				panic(expectedError)
			},
		}
	)

	registrar.On("Watch").Once().Return(watch, nil)
	watch.On("Endpoints").Once().Return(expectedEndpoints)
	watch.On("Close").Once()

	assert.NoError(subscription.Run())

	timer := time.NewTimer(time.Second)
	defer timer.Stop()
	select {
	case <-listenerCalled:
		// passing case
		registrar.AssertExpectations(t)
		watch.AssertExpectations(t)

		// Cancel should be idempotent
		subscription.Cancel()
		assert.Equal(ErrorNotRunning, subscription.Cancel())

	case <-timer.C:
		assert.Fail("The listener was not called")<commit_msg>Fixed the ordering of assertions<commit_after>package service

import (
	"errors"
	"sync/atomic"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func testSubscriptionWatchError(t *testing.T) {
	var (
		assert        = assert.New(t)
		expectedError = errors.New("expected")
		registrar     = new(mockRegistrar)

		subscription = Subscription{
			Registrar: registrar,
			Listener: func([]string) {
				assert.Fail("The listener should not have been called")
			},
		}
	)

	registrar.On("Watch").Once().Return(nil, expectedError)

	assert.Equal(expectedError, subscription.Run())
	assert.Equal(ErrorNotRunning, subscription.Cancel())

	registrar.AssertExpectations(t)
}

func testSubscriptionListenerPanic(t *testing.T) {
	var (
		assert        = assert.New(t)
		expectedError = errors.New("expected")

		watch     = new(mockWatch)
		registrar = new(mockRegistrar)

		expectedEndpoints = []string{"expected endpoint"}
		listenerCalled    = make(chan struct{})

		subscription = Subscription{
			Registrar: registrar,
			Listener: func(endpoints []string) {
				defer close(listenerCalled)
				assert.Equal(expectedEndpoints, endpoints)
				panic(expectedError)
			},
		}
	)

	registrar.On("Watch").Once().Return(watch, nil)
	watch.On("Endpoints").Once().Return(expectedEndpoints)
	watch.On("Close")

	assert.NoError(subscription.Run())

	timer := time.NewTimer(time.Second)
	defer timer.Stop()
	select {
	case <-listenerCalled:
		// Cancel should be idempotent
		subscription.Cancel()
		assert.Equal(ErrorNotRunning, subscription.Cancel())

		// passing case
		registrar.AssertExpectations(t)
		watch.AssertExpectations(t)

	case <-timer.C:
		assert.Fail("The listener was not called")
{'fraction_non_alphanumeric': 0.13106508875739645, 'fraction_numerical': 0.0, 'mean_word_length': 4.400958466453674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6932506', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1086, 'n_words': 199}