github.com/thiagoyeds/go-cloud@v0.26.0/docstore/drivertest/util.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package drivertest
    16  
    17  import (
    18  	"math/rand"
    19  	"sync"
    20  
    21  	"github.com/google/uuid"
    22  	"gocloud.dev/docstore/driver"
    23  )
    24  
    25  // MakeUniqueStringDeterministicForTesting uses a specified seed value to
    26  // produce the same sequence of values in driver.UniqueString for testing.
    27  //
    28  // Call when running tests that will be replayed.
    29  func MakeUniqueStringDeterministicForTesting(seed int64) {
    30  	r := &randReader{r: rand.New(rand.NewSource(seed))}
    31  	uuid.SetRand(r)
    32  }
    33  
    34  type randReader struct {
    35  	mu sync.Mutex
    36  	r  *rand.Rand
    37  }
    38  
    39  func (r *randReader) Read(buf []byte) (int, error) {
    40  	r.mu.Lock()
    41  	defer r.mu.Unlock()
    42  	return r.r.Read(buf)
    43  }
    44  
    45  // MustDocument is like driver.NewDocument, but panics on error.
    46  func MustDocument(doc interface{}) driver.Document {
    47  	dd, err := driver.NewDocument(doc)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	return dd
    52  }