...

Source file src/github.com/Shopify/go-storage/slow_wrapper.go

Documentation: github.com/Shopify/go-storage

     1  package storage
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"time"
     7  )
     8  
     9  // NewSlowWrapper creates an artificially slow FS.
    10  // Probably only useful for testing.
    11  func NewSlowWrapper(fs FS, readDelay time.Duration, writeDelay time.Duration) FS {
    12  	return &slowWrapper{
    13  		fs:         fs,
    14  		readDelay:  readDelay,
    15  		writeDelay: writeDelay,
    16  	}
    17  }
    18  
    19  type slowWrapper struct {
    20  	fs         FS
    21  	readDelay  time.Duration
    22  	writeDelay time.Duration
    23  }
    24  
    25  func (fs *slowWrapper) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
    26  	select {
    27  	case <-time.After(fs.readDelay):
    28  		return fs.fs.Open(ctx, path, options)
    29  	case <-ctx.Done():
    30  		return nil, ctx.Err()
    31  	}
    32  }
    33  
    34  func (fs *slowWrapper) Walk(ctx context.Context, path string, fn WalkFn) error {
    35  	select {
    36  	case <-time.After(fs.readDelay):
    37  		return fs.fs.Walk(ctx, path, fn)
    38  	case <-ctx.Done():
    39  		return ctx.Err()
    40  	}
    41  }
    42  
    43  func (fs *slowWrapper) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error) {
    44  	select {
    45  	case <-time.After(fs.readDelay):
    46  		return fs.fs.Attributes(ctx, path, options)
    47  	case <-ctx.Done():
    48  		return nil, ctx.Err()
    49  	}
    50  }
    51  
    52  func (fs *slowWrapper) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
    53  	select {
    54  	case <-time.After(fs.writeDelay):
    55  		return fs.fs.Create(ctx, path, options)
    56  	case <-ctx.Done():
    57  		return nil, ctx.Err()
    58  	}
    59  }
    60  
    61  func (fs *slowWrapper) Delete(ctx context.Context, path string) error {
    62  	select {
    63  	case <-time.After(fs.writeDelay):
    64  		return fs.fs.Delete(ctx, path)
    65  	case <-ctx.Done():
    66  		return ctx.Err()
    67  	}
    68  }
    69  
    70  func (fs *slowWrapper) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error) {
    71  	select {
    72  	case <-time.After(fs.readDelay):
    73  		return fs.fs.URL(ctx, path, options)
    74  	case <-ctx.Done():
    75  		return "", ctx.Err()
    76  	}
    77  }
    78  

View as plain text