github.com/cornelk/go-cloud@v0.17.1/blob/oc_test.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 blob_test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/cornelk/go-cloud/blob"
    22  	"github.com/cornelk/go-cloud/blob/memblob"
    23  	"github.com/cornelk/go-cloud/gcerrors"
    24  	"github.com/cornelk/go-cloud/internal/oc"
    25  	"github.com/cornelk/go-cloud/internal/testing/octest"
    26  	"github.com/google/go-cmp/cmp"
    27  	"go.opencensus.io/stats/view"
    28  	"go.opencensus.io/tag"
    29  )
    30  
    31  func TestOpenCensus(t *testing.T) {
    32  	ctx := context.Background()
    33  	te := octest.NewTestExporter(blob.OpenCensusViews)
    34  	defer te.Unregister()
    35  
    36  	bytes := []byte("foo")
    37  	b := memblob.OpenBucket(nil)
    38  	defer b.Close()
    39  	if err := b.WriteAll(ctx, "key", bytes, nil); err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if _, err := b.ReadAll(ctx, "key"); err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	if _, err := b.Attributes(ctx, "key"); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if err := b.Delete(ctx, "key"); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if _, err := b.ReadAll(ctx, "noSuchKey"); err == nil {
    52  		t.Fatal("got nil, want error")
    53  	}
    54  
    55  	const driver = "github.com/cornelk/go-cloud/blob/memblob"
    56  
    57  	diff := octest.Diff(te.Spans(), te.Counts(), "github.com/cornelk/go-cloud/blob", driver, []octest.Call{
    58  		{Method: "NewWriter", Code: gcerrors.OK},
    59  		{Method: "NewRangeReader", Code: gcerrors.OK},
    60  		{Method: "Attributes", Code: gcerrors.OK},
    61  		{Method: "Delete", Code: gcerrors.OK},
    62  		{Method: "NewRangeReader", Code: gcerrors.NotFound},
    63  	})
    64  	if diff != "" {
    65  		t.Error(diff)
    66  	}
    67  
    68  	// Find and verify the bytes read/written metrics.
    69  	var sawRead, sawWritten bool
    70  	tags := []tag.Tag{{Key: oc.ProviderKey, Value: driver}}
    71  	for !sawRead || !sawWritten {
    72  		data := <-te.Stats
    73  		switch data.View.Name {
    74  		case "github.com/cornelk/go-cloud/blob/bytes_read":
    75  			if sawRead {
    76  				continue
    77  			}
    78  			sawRead = true
    79  		case "github.com/cornelk/go-cloud/blob/bytes_written":
    80  			if sawWritten {
    81  				continue
    82  			}
    83  			sawWritten = true
    84  		default:
    85  			continue
    86  		}
    87  		if diff := cmp.Diff(data.Rows[0].Tags, tags, cmp.AllowUnexported(tag.Key{})); diff != "" {
    88  			t.Errorf("tags for %s: %s", data.View.Name, diff)
    89  			continue
    90  		}
    91  		sd, ok := data.Rows[0].Data.(*view.SumData)
    92  		if !ok {
    93  			t.Errorf("%s: data is %T, want SumData", data.View.Name, data.Rows[0].Data)
    94  			continue
    95  		}
    96  		if got := int(sd.Value); got < len(bytes) {
    97  			t.Errorf("%s: got %d, want at least %d", data.View.Name, got, len(bytes))
    98  		}
    99  	}
   100  }