99 "math/rand"
1010 "os"
1111 "runtime"
12+ "sync/atomic"
1213 "testing"
1314 "time"
1415
@@ -21,7 +22,7 @@ import (
2122)
2223
2324// ContentSuite runs a test suite on the content store given a factory function.
24- func ContentSuite (t * testing.T , name string , storeFn func (ctx context.Context , root string ) (content.Store , func () error , error )) {
25+ func ContentSuite (t * testing.T , name string , storeFn func (ctx context.Context , root string ) (context. Context , content.Store , func () error , error )) {
2526 t .Run ("Writer" , makeTest (t , name , storeFn , checkContentStoreWriter ))
2627 t .Run ("UploadStatus" , makeTest (t , name , storeFn , checkUploadStatus ))
2728 t .Run ("Resume" , makeTest (t , name , storeFn , checkResumeWriter ))
@@ -33,7 +34,7 @@ func ContentSuite(t *testing.T, name string, storeFn func(ctx context.Context, r
3334 t .Run ("Labels" , makeTest (t , name , storeFn , checkLabels ))
3435}
3536
36- func makeTest (t * testing.T , name string , storeFn func (ctx context.Context , root string ) (content.Store , func () error , error ), fn func (ctx context.Context , t * testing.T , cs content.Store )) func (t * testing.T ) {
37+ func makeTest (t * testing.T , name string , storeFn func (ctx context.Context , root string ) (context. Context , content.Store , func () error , error ), fn func (ctx context.Context , t * testing.T , cs content.Store )) func (t * testing.T ) {
3738 return func (t * testing.T ) {
3839 ctx := namespaces .WithNamespace (context .Background (), name )
3940
@@ -43,7 +44,7 @@ func makeTest(t *testing.T, name string, storeFn func(ctx context.Context, root
4344 }
4445 defer os .RemoveAll (tmpDir )
4546
46- cs , cleanup , err := storeFn (ctx , tmpDir )
47+ ctx , cs , cleanup , err := storeFn (ctx , tmpDir )
4748 if err != nil {
4849 t .Fatal (err )
4950 }
@@ -63,28 +64,28 @@ var labels = map[string]string{
6364}
6465
6566func checkContentStoreWriter (ctx context.Context , t * testing.T , cs content.Store ) {
66- c1 , d1 := createContent (256 , 1 )
67+ c1 , d1 := createContent (256 )
6768 w1 , err := cs .Writer (ctx , "c1" , 0 , "" )
6869 if err != nil {
6970 t .Fatal (err )
7071 }
7172 defer w1 .Close ()
7273
73- c2 , d2 := createContent (256 , 2 )
74+ c2 , d2 := createContent (256 )
7475 w2 , err := cs .Writer (ctx , "c2" , int64 (len (c2 )), "" )
7576 if err != nil {
7677 t .Fatal (err )
7778 }
7879 defer w2 .Close ()
7980
80- c3 , d3 := createContent (256 , 3 )
81+ c3 , d3 := createContent (256 )
8182 w3 , err := cs .Writer (ctx , "c3" , 0 , d3 )
8283 if err != nil {
8384 t .Fatal (err )
8485 }
8586 defer w3 .Close ()
8687
87- c4 , d4 := createContent (256 , 4 )
88+ c4 , d4 := createContent (256 )
8889 w4 , err := cs .Writer (ctx , "c4" , int64 (len (c4 )), d4 )
8990 if err != nil {
9091 t .Fatal (err )
@@ -163,7 +164,7 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
163164
164165 var (
165166 ref = "cb"
166- cb , dgst = createContent (256 , 10 )
167+ cb , dgst = createContent (256 )
167168 first , second = cb [:128 ], cb [128 :]
168169 )
169170
@@ -223,7 +224,7 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
223224}
224225
225226func checkUploadStatus (ctx context.Context , t * testing.T , cs content.Store ) {
226- c1 , d1 := createContent (256 , 17 )
227+ c1 , d1 := createContent (256 )
227228
228229 preStart := time .Now ()
229230 w1 , err := cs .Writer (ctx , "c1" , 256 , d1 )
@@ -292,7 +293,7 @@ func checkUploadStatus(ctx context.Context, t *testing.T, cs content.Store) {
292293}
293294
294295func checkLabels (ctx context.Context , t * testing.T , cs content.Store ) {
295- c1 , d1 := createContent (256 , 19 )
296+ c1 , d1 := createContent (256 )
296297
297298 w1 , err := cs .Writer (ctx , "c1" , 256 , d1 )
298299 if err != nil {
@@ -365,7 +366,7 @@ func checkResume(rf func(context.Context, content.Writer, []byte, int64, int64,
365366
366367 for i , size := range sizes {
367368 for j , tp := range truncations {
368- b , d := createContent (size , int64 ( i * len ( truncations ) + j ) )
369+ b , d := createContent (size )
369370 limit := int64 (float64 (size ) * tp )
370371 ref := fmt .Sprintf ("ref-%d-%d" , i , j )
371372
@@ -548,7 +549,14 @@ func checkInfo(ctx context.Context, cs content.Store, d digest.Digest, expected
548549 return nil
549550}
550551
551- func createContent (size , seed int64 ) ([]byte , digest.Digest ) {
552+ var contentSeed int64
553+
554+ func createContent (size int64 ) ([]byte , digest.Digest ) {
555+ // each time we call this, we want to get a different seed, but it should
556+ // be related to the intitialization order and fairly consistent between
557+ // test runs. An atomic integer works just good enough for this.
558+ seed := atomic .AddInt64 (& contentSeed , 1 )
559+
552560 b , err := ioutil .ReadAll (io .LimitReader (rand .New (rand .NewSource (seed )), size ))
553561 if err != nil {
554562 panic (err )
0 commit comments