<commit_before>package rbuf

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	cv "github.com/smartystreets/goconvey/convey"
)

// new tests just for atomic version

func TestAtomicRingBufSafeConcurrently001(t *testing.T) {

	cv.Convey("Given an AtomicFixedSizeRingBuf", t, func() {
		cv.Convey("concurrent access to the ring should never corrupt it or deadlock, so writing bytes numbered in order should result in reading those bytes in the same numeric order. NB: This test takes about 30 seconds to run on my laptop, with maxRingSize = 20 and N = 10.\n\n", func() {

			maxRingSize := 20
			for ringSize := 1; ringSize < maxRingSize; ringSize++ {
				b := NewAtomicFixedSizeRingBuf(ringSize)

				for bufsz := 1; bufsz < 2*ringSize+1; bufsz++ {
					fmt.Printf("*** testing ringSize = %d  and Read/Write with bufsz = %d\n", ringSize, bufsz)

					writerDone := make(chan bool)
					readerDone := make(chan bool)

					//N := 255 // biggest N that fits in a byte is 255
					N := 10
					bufsz := 1
					go func() {
						// writer, write numbers from 0..N in order.
						by := make([]byte, bufsz)
						lastWritten := -1
						for lastWritten < N {
							for j := 0; j < bufsz; j++ {
								by[j] = byte(lastWritten + 1 + j)
							}
							n, err := b.Write(by)
							if err != nil && err.Error() != "short write" {
								panic(fmt.Errorf("wrote n=%d, error: '%s'", n, err))
							}
							if n > 0 {
								//fmt.Printf("writer wrote from %d .. %d\n", lastWritten+1, lastWritten+n)
							}
							lastWritten += n
						}
						close(writerDone)
					}()

					go func() {
						// reader, expect to read 0..N in order
						by := make([]byte, bufsz)
						lastRead := -1
						for lastRead < N {
							n, err := b.Read(by)
							if err != nil && err.Error() != "EOF" {
								panic(err)
							}
							for j := 0; j < n; j++ {
								//cv.So(by[j], cv.ShouldEqual, lastRead+1+j)<commit_msg>atg. default tests run quickly<commit_after>package rbuf

import (
	"bytes"
	"fmt"
	"io"
	"testing"

	cv "github.com/smartystreets/goconvey/convey"
)

// new tests just for atomic version

func TestAtomicRingBufSafeConcurrently001(t *testing.T) {

	cv.Convey("Given an AtomicFixedSizeRingBuf", t, func() {
		cv.Convey("concurrent access to the ring should never corrupt it or deadlock, so writing bytes numbered in order should result in reading those bytes in the same numeric order.\n\n", func() {

			// the ringSize +=3 and bufsz +=7 result in sampling instead of
			// exhaustive checking, but that should be fine.
			maxRingSize := 20
			for ringSize := 5; ringSize < maxRingSize; ringSize += 3 {
				b := NewAtomicFixedSizeRingBuf(ringSize)

				for bufsz := 3; bufsz < 2*ringSize+1; bufsz += 7 {
					fmt.Printf("*** testing ringSize = %d  and Read/Write with bufsz = %d\n", ringSize, bufsz)

					writerDone := make(chan bool)
					readerDone := make(chan bool)

					//N := 255 // biggest N that fits in a byte is 255
					N := 20
					go func() {
						// writer, write numbers from 0..N in order.
						by := make([]byte, bufsz)
						lastWritten := -1
						for lastWritten < N {
							for j := 0; j < bufsz; j++ {
								by[j] = byte(lastWritten + 1 + j)
							}
							n, err := b.Write(by)
							if err != nil && err.Error() != "short write" {
								panic(fmt.Errorf("wrote n=%d, error: '%s'", n, err))
							}
							if n > 0 {
								//fmt.Printf("writer wrote from %d .. %d\n", lastWritten+1, lastWritten+n)
							}
							lastWritten += n
						}
						close(writerDone)
					}()

					go func() {
						// reader, expect to read 0..N in order
						by := make([]byte, bufsz)
						lastRead := -1
						for lastRead < N {
							n, err := b.Read(by)
							if err != nil && err.Error() != "EOF" {
								panic(err)
							}
							for j := 0; j < n; j++ {
								//cv.So(by[j], cv.ShouldEqual, lastRead+1+j)
{'fraction_non_alphanumeric': 0.1415929203539823, 'fraction_numerical': 0.01691827173347215, 'mean_word_length': 2.27901023890785, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '1026151', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1289, 'n_words': 419}