<commit_before>package main

import (
	"testing"
	"time"
)

func fatalRun(t *testing.T, msg string) func() (interface{}, error) {
	return func() (interface{}, error) {
		t.Fatal(msg)
		return nil, nil
	}
}

func fatalSetResult(t *testing.T, msg string) func(interface{}, error) {
	return func(_ interface{}, _ error) {
		t.Fatal(msg)
	}
}

func TestNewRequesterAndStop(t *testing.T) {
	var requester Requester = NewRequester(10, 10)
	defer requester.Stop()
	if requester == nil {
		t.Errorf("the returned requester is nil")
	}
}

func TestAddRequestRunsARequest(t *testing.T) {
	var requester = NewRequester(10, 10)
	defer requester.Stop()
	var ran = make(chan struct{})
	r := &request{
		id:        "a",
		cacheable: true,
		run: func() (interface{}, error) {
			close(ran)
			return "result1", nil
		},
		setResult: okSetResult,
	}
	requester.AddRequest(r)
	<-ran
}

func TestNonCacheableRequests(t *testing.T) {
	var requester = NewRequester(10, 10)
	defer requester.Stop()
	var expected1, expected2 = "foo", "bar"
	var setted = make(chan struct{})
	r := &request{
		id:        "a",
		cacheable: false,
		run: func() (interface{}, error) {
			return expected1, nil
		},
		setResult: nil,
	}
	requester.AddRequest(r)
	r = &request{
		id:        "a",
		cacheable: false,
		run: func() (interface{}, error) {
			defer close(setted)
			return expected2, nil
		},
		setResult: nil,
	}
	requester.AddRequest(r)
	<-setted
}

type request struct {
	id         string
	cacheable  bool
	alreadyRan bool
	run        func() (interface{}, error)
	setResult  func(interface{}, error)
}

func (r *request) ID() string {
	return r.id
}

func (r *request) Run() (interface{}, error) {
	if r.alreadyRan {
		panic("Run after Run or SetResult")
	}
	r.alreadyRan = true
	return r.run()
}

func (r *request) Cacheable() bool {
	return r.cacheable
}

func (r *request) SetResult(result interface{}, err error) {
	if r.alreadyRan {
		panic("SetResult after Run or SetResult")
	}
	r.alreadyRan = true
	r.setResult(result, err)
}

func okSetResult(result interface{}, err error) {
}

func TestStopWithQueueFromForum(t *testing.T) {
	t.Parallel()
	var (
		requester    = NewRequester(1000, 2)
		r1Started    = make(chan struct{})
		r1Continue   = make(chan struct{})
		r1Finished   = make(chan struct{})
		r2SetResult  = make(chan struct{})
		r4Started    = make(chan struct{})
		r4Continue   = make(chan struct{})
		r4Finished   = make(chan struct{})
		stopStarted  = make(chan struct{})
		stopFinished = make(chan struct{})
	)
	r1 := &request{ // Should Run
		id:        "a",
		cacheable: true,
		run: func() (interface{}, error) {
			close(r1Started)
			<-r1Continue
			defer close(r1Finished)
			return "result1", nil
		},
		setResult: fatalSetResult(t, "r4 should be runned not SetResulted"),
	}

	r2 := &request{
		id:        "a",
		cacheable: true,
		run:       fatalRun(t, "r3 should not be Ran as it should be cached"),<commit_msg>task04: move all helper function/types before the tests<commit_after>package main

import (
	"testing"
	"time"
)

type request struct {
	id         string
	cacheable  bool
	alreadyRan bool
	run        func() (interface{}, error)
	setResult  func(interface{}, error)
}

func (r *request) ID() string {
	return r.id
}

func (r *request) Run() (interface{}, error) {
	if r.alreadyRan {
		panic("Run after Run or SetResult")
	}
	r.alreadyRan = true
	return r.run()
}

func (r *request) Cacheable() bool {
	return r.cacheable
}

func (r *request) SetResult(result interface{}, err error) {
	if r.alreadyRan {
		panic("SetResult after Run or SetResult")
	}
	r.alreadyRan = true
	r.setResult(result, err)
}

func okSetResult(result interface{}, err error) {}

func fatalRun(t *testing.T, msg string) func() (interface{}, error) {
	return func() (interface{}, error) {
		t.Fatal(msg)
		return nil, nil
	}
}

func fatalSetResult(t *testing.T, msg string) func(interface{}, error) {
	return func(_ interface{}, _ error) {
		t.Fatal(msg)
	}
}

func TestNewRequesterAndStop(t *testing.T) {
	var requester Requester = NewRequester(10, 10)
	defer requester.Stop()
	if requester == nil {
		t.Errorf("the returned requester is nil")
	}
}

func TestAddRequestRunsARequest(t *testing.T) {
	var requester = NewRequester(10, 10)
	defer requester.Stop()
	var ran = make(chan struct{})
	r := &request{
		id:        "a",
		cacheable: true,
		run: func() (interface{}, error) {
			close(ran)
			return "result1", nil
		},
		setResult: okSetResult,
	}
	requester.AddRequest(r)
	<-ran
}

func TestNonCacheableRequests(t *testing.T) {
	var requester = NewRequester(10, 10)
	defer requester.Stop()
	var expected1, expected2 = "foo", "bar"
	var setted = make(chan struct{})
	r := &request{
		id:        "a",
		cacheable: false,
		run: func() (interface{}, error) {
			return expected1, nil
		},
		setResult: nil,
	}
	requester.AddRequest(r)
	r = &request{
		id:        "a",
		cacheable: false,
		run: func() (interface{}, error) {
			defer close(setted)
			return expected2, nil
		},
		setResult: nil,
	}
	requester.AddRequest(r)
	<-setted
}
func TestStopWithQueueFromForum(t *testing.T) {
	t.Parallel()
	var (
		requester    = NewRequester(1000, 2)
		r1Started    = make(chan struct{})
		r1Continue   = make(chan struct{})
		r1Finished   = make(chan struct{})
		r2SetResult  = make(chan struct{})
		r4Started    = make(chan struct{})
		r4Continue   = make(chan struct{})
		r4Finished   = make(chan struct{})
		stopStarted  = make(chan struct{})
		stopFinished = make(chan struct{})
	)
	r1 := &request{ // Should Run
		id:        "a",
		cacheable: true,
		run: func() (interface{}, error) {
			close(r1Started)
			<-r1Continue
			defer close(r1Finished)
			return "result1", nil
		},
		setResult: fatalSetResult(t, "r4 should be runned not SetResulted"),
	}

	r2 := &request{
		id:        "a",
		cacheable: true,
		run:       fatalRun(t, "r3 should not be Ran as it should be cached"),
{'fraction_non_alphanumeric': 0.1420299727520436, 'fraction_numerical': 0.012942779291553134, 'mean_word_length': 3.613511390416339, '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': '1970595', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2142, 'n_words': 576}