github.com/thiagoyeds/go-cloud@v0.26.0/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/google/go-cmp/cmp"
    22  	"go.opencensus.io/stats/view"
    23  	"go.opencensus.io/tag"
    24  	"gocloud.dev/blob"
    25  	"gocloud.dev/blob/memblob"
    26  	"gocloud.dev/gcerrors"
    27  	"gocloud.dev/internal/oc"
    28  	"gocloud.dev/internal/testing/octest"
    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.ListPage(ctx, blob.FirstPageToken, 3, nil); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if err := b.Delete(ctx, "key"); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if _, err := b.ReadAll(ctx, "noSuchKey"); err == nil {
    55  		t.Fatal("got nil, want error")
    56  	}
    57  
    58  	const driver = "gocloud.dev/blob/memblob"
    59  
    60  	diff := octest.Diff(te.Spans(), te.Counts(), "gocloud.dev/blob", driver, []octest.Call{
    61  		{Method: "NewWriter", Code: gcerrors.OK},
    62  		{Method: "NewRangeReader", Code: gcerrors.OK},
    63  		{Method: "Attributes", Code: gcerrors.OK},
    64  		{Method: "ListPage", Code: gcerrors.OK},
    65  		{Method: "Delete", Code: gcerrors.OK},
    66  		{Method: "NewRangeReader", Code: gcerrors.NotFound},
    67  	})
    68  	if diff != "" {
    69  		t.Error(diff)
    70  	}
    71  
    72  	// Find and verify the bytes read/written metrics.
    73  	var sawRead, sawWritten bool
    74  	tags := []tag.Tag{{Key: oc.ProviderKey, Value: driver}}
    75  	for !sawRead || !sawWritten {
    76  		data := <-te.Stats
    77  		switch data.View.Name {
    78  		case "gocloud.dev/blob/bytes_read":
    79  			if sawRead {
    80  				continue
    81  			}
    82  			sawRead = true
    83  		case "gocloud.dev/blob/bytes_written":
    84  			if sawWritten {
    85  				continue
    86  			}
    87  			sawWritten = true
    88  		default:
    89  			continue
    90  		}
    91  		if diff := cmp.Diff(data.Rows[0].Tags, tags, cmp.AllowUnexported(tag.Key{})); diff != "" {
    92  			t.Errorf("tags for %s: %s", data.View.Name, diff)
    93  			continue
    94  		}
    95  		sd, ok := data.Rows[0].Data.(*view.SumData)
    96  		if !ok {
    97  			t.Errorf("%s: data is %T, want SumData", data.View.Name, data.Rows[0].Data)
    98  			continue
    99  		}
   100  		if got := int(sd.Value); got < len(bytes) {
   101  			t.Errorf("%s: got %d, want at least %d", data.View.Name, got, len(bytes))
   102  		}
   103  	}
   104  }