Files
kitchensink/pkg/grpc/longmsg.go
David Vennik 8033b8cdbb Concurrency test part way done
Discovered a concurrent map read/write error
2022-05-11 11:46:33 +03:00

39 lines
1.1 KiB
Go

package grpc
import (
"encoding/hex"
)
// MakeLongMessage creates a message by concatenating a collection of hex
// encoded strings into one, and then repeating the concatenation with the
// resultant bytes for the number of given repetitions
func MakeLongMessage(repetitions int, input []string) (output []byte) {
totalLen := 0
// Accumulate the length of all the strings, each character of the string
// represents 4 bytes so the byte length is half of this
for i := range input {
totalLen += len(input[i]) / 2
}
// Make one repetition as a single slice
oneRep := make([]byte, totalLen)
cursor := 0
for i := range input {
bytes, err := hex.DecodeString(input[i])
if err != nil {
panic("something wrong with input")
}
bLen := len(bytes)
copy(oneRep[cursor:cursor+bLen], bytes)
cursor += bLen
}
// Copy the single repetition over and over onto a buffer of this size times
// the number of repetitions requested
output = make([]byte, totalLen*repetitions)
cursor = 0
for reps := 0; reps < repetitions; reps++ {
copy(output[cursor:cursor+totalLen], oneRep)
cursor += totalLen
}
return
}