github.com/Comcast/plax@v0.8.32/dsl/ctx_test.go (about)

     1  /*
     2   * Copyright 2021 Comcast Cable Communications Management, LLC
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * SPDX-License-Identifier: Apache-2.0
    17   */
    18  
    19  package dsl
    20  
    21  import (
    22  	"fmt"
    23  	"log"
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  )
    28  
    29  func TestCtxCancel(t *testing.T) {
    30  	ctx, cancel := NewCtx(nil).WithCancel()
    31  	cancel()
    32  	select {
    33  	case <-ctx.Done():
    34  	default:
    35  		t.Fatal("not canceled")
    36  	}
    37  }
    38  
    39  func TestCtxTimeout(t *testing.T) {
    40  	ctx, _ := NewCtx(nil).WithTimeout(50 * time.Millisecond)
    41  	time.Sleep(100 * time.Millisecond)
    42  	select {
    43  	case <-ctx.Done():
    44  	default:
    45  		t.Fatal("didn't time out")
    46  	}
    47  }
    48  
    49  type TestLogger struct {
    50  	lines []string
    51  }
    52  
    53  func NewTestLogger() *TestLogger {
    54  	return &TestLogger{
    55  		lines: make([]string, 0, 32),
    56  	}
    57  }
    58  
    59  func (l *TestLogger) Printf(format string, args ...interface{}) {
    60  	line := fmt.Sprintf(format, args...)
    61  	l.lines = append(l.lines, line)
    62  }
    63  
    64  func TestRedactBasic(t *testing.T) {
    65  	ctx := NewCtx(nil)
    66  	if err := ctx.AddRedaction("tacos?"); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	ctx.Redact = true
    70  	l := NewTestLogger()
    71  	ctx.Logger = l
    72  	ctx.Logf(`Please don't say "tacos".`)
    73  
    74  	if len(l.lines) == 0 {
    75  		t.Fatal(0)
    76  	}
    77  
    78  	for _, line := range l.lines {
    79  		if strings.Contains(line, "tacos") {
    80  			t.Fatal(line)
    81  		}
    82  	}
    83  }
    84  
    85  func TestRedactBindings(t *testing.T) {
    86  	ctx := NewCtx(nil)
    87  	ctx.Redact = true
    88  	l := NewTestLogger()
    89  	ctx.Logger = l
    90  
    91  	tst := NewTest(ctx, "test", nil)
    92  	tst.Bindings["?X_NEVERSAY"] = "I love crêpes."
    93  	if err := tst.bindingRedactions(ctx); err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	line := `Never say "{?X_NEVERSAY}"`
    97  	var err error
    98  	line, err = tst.Bindings.StringSub(ctx, line)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	ctx.Logf("%s", line)
   104  
   105  	if len(l.lines) == 0 {
   106  		t.Fatal(0)
   107  	}
   108  
   109  	line = l.lines[0]
   110  
   111  	log.Println(line)
   112  
   113  	if strings.Contains(line, "crêpes") {
   114  		t.Fatal(line)
   115  	}
   116  	if !strings.Contains(line, "redacted") {
   117  		t.Fatal(line)
   118  	}
   119  }