Skip to content

Commit

Permalink
Data race proof
Browse files Browse the repository at this point in the history
  • Loading branch information
James Crasta committed Feb 8, 2016
1 parent 48ad557 commit 008b61b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions datum_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package avro
import (
"bytes"
"fmt"
"sync"
"testing"
)

Expand Down Expand Up @@ -330,6 +331,53 @@ func TestGenericDatumReaderEmptyArray(t *testing.T) {
assert(t, rec.Get("map1"), nil)
}

var schemaEnumA = MustParseSchema(`
{"type": "record", "name": "PlayingCard",
"fields": [
{"name": "type", "type": {"type": "enum", "name": "Type", "symbols":["HEART", "SPADE", "CLUB"]}}
]}`)
var schemaEnumB = MustParseSchema(`
{"type": "record", "name": "Car",
"fields": [
{"name": "drive", "type": {"type": "enum", "name": "DriveSystem", "symbols":["FWD", "RWD", "AWD"]}}
]}`)

func TestEnumCachingRace(t *testing.T) {
enumRaceTest(t, []Schema{schemaEnumA})
}

func TestEnumCachingRace2(t *testing.T) {
enumRaceTest(t, []Schema{schemaEnumA, schemaEnumB})
}

func enumRaceTest(t *testing.T, schemas []Schema) {
var buf bytes.Buffer
enc := NewBinaryEncoder(&buf)
enc.WriteInt(2)

parallelF(20, 100, func(routine, loop int) {
var dest GenericRecord
schema := schemas[routine%len(schemas)]
reader := NewGenericDatumReader()
reader.SetSchema(schema)
reader.Read(&dest, NewBinaryDecoder(buf.Bytes()))
})

}

func parallelF(numRoutines, numLoops int, f func(routine, loop int)) {
var wg sync.WaitGroup
wg.Add(numRoutines)
for i := 0; i < numRoutines; i++ {
go func(routine int) {
defer wg.Done()
for loop := 0; loop < numLoops; loop++ {
f(routine, loop)
}
}(i)
}
}

func BenchmarkSpecificDatumReader_complex(b *testing.B) {
var dest complex
specificReaderBenchComplex(b, &dest)
Expand Down

0 comments on commit 008b61b

Please sign in to comment.