github.com/thiagoyeds/go-cloud@v0.26.0/runtimevar/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 runtimevar_test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"go.opencensus.io/stats/view"
    22  	"gocloud.dev/internal/oc"
    23  	"gocloud.dev/internal/testing/octest"
    24  	"gocloud.dev/runtimevar"
    25  	"gocloud.dev/runtimevar/constantvar"
    26  )
    27  
    28  func TestOpenCensus(t *testing.T) {
    29  	ctx := context.Background()
    30  	te := octest.NewTestExporter(runtimevar.OpenCensusViews)
    31  	defer te.Unregister()
    32  
    33  	v := constantvar.New(1)
    34  	defer v.Close()
    35  	if _, err := v.Watch(ctx); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	cctx, cancel := context.WithCancel(ctx)
    39  	cancel()
    40  	_, _ = v.Watch(cctx)
    41  
    42  	seen := false
    43  	const driver = "gocloud.dev/runtimevar/constantvar"
    44  	for _, row := range te.Counts() {
    45  		if _, ok := row.Data.(*view.CountData); !ok {
    46  			continue
    47  		}
    48  		if row.Tags[0].Key == oc.ProviderKey && row.Tags[0].Value == driver {
    49  			seen = true
    50  			break
    51  		}
    52  	}
    53  	if !seen {
    54  		t.Errorf("did not see count row with provider=%s", driver)
    55  	}
    56  }