<commit_before><commit_msg>kafka: fake<commit_after>package kafka

import (
	"fmt"

	"github.com/gocql/gocql"

	"github.com/dollarshaveclub/furan/generated/lib"
)

type FakeEventBus struct {
	c chan *lib.BuildEvent
}

var _ EventBusProducer = &FakeEventBus{}
var _ EventBusConsumer = &FakeEventBus{}

func NewFakeEventBusProducer(capacity uint) *FakeEventBus {
	return &FakeEventBus{
		c: make(chan *lib.BuildEvent, capacity),
	}
}

func (fb *FakeEventBus) PublishEvent(event *lib.BuildEvent) error {
	select {
	case fb.c <- event:
		return nil
	default:
		return fmt.Errorf("channel full")
	}
}

func (fb *FakeEventBus) SubscribeToTopic(c chan<- *lib.BuildEvent, done <-chan struct{}, id gocql.UUID) error {
	if fb.c == nil {
		return fmt.Errorf("nil channel")
	}
	go func() {
		for {
			select {
			case event, ok := <-fb.c:
				if !ok {
					return
				}
				if event != nil && event.BuildId == id.String() {
					c <- event
				}
			case <-done:
				return
			}
		}
	}()
	return nil
}
{'fraction_non_alphanumeric': 0.13765182186234817, 'fraction_numerical': 0.0, 'mean_word_length': 3.3955555555555557, 'pattern_counts': {'":': 0, '<': 9, '<?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': '7511739', 'n_tokens_mistral': 415, 'n_tokens_neox': 366, 'n_words': 86}