...

Source file src/github.com/growthbook/growthbook-golang/subscriptions_test.go

Documentation: github.com/growthbook/growthbook-golang

     1  package growthbook
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestSubscriptionsSubscribe(t *testing.T) {
     9  	context := NewContext().WithAttributes(Attributes{"id": "1"})
    10  	gb := New(context)
    11  	exp1 := NewExperiment("experiment-1").WithVariations("result1", "result2")
    12  
    13  	var savedExp *Experiment
    14  	called := 0
    15  	gb.Subscribe(func(exp *Experiment, result *Result) {
    16  		savedExp = exp
    17  		called++
    18  	})
    19  
    20  	gb.Run(exp1)
    21  	gb.Run(exp1)
    22  
    23  	if !reflect.DeepEqual(savedExp, exp1) {
    24  		t.Errorf("unexpected experiment value: %v", savedExp)
    25  	}
    26  	// Subscription only gets triggered once for repeated experiment
    27  	// runs.
    28  	if called != 1 {
    29  		t.Errorf("expected called = 1, got called = %d", called)
    30  	}
    31  
    32  	savedExp = nil
    33  	called = 0
    34  
    35  	gb.ClearSavedResults()
    36  	gb.Run(exp1)
    37  	// Change attributes to change experiment result so subscription
    38  	// gets triggered twice.
    39  	gb.WithAttributes(Attributes{"id": "3"})
    40  	gb.Run(exp1)
    41  
    42  	if !reflect.DeepEqual(savedExp, exp1) {
    43  		t.Errorf("unexpected experiment value: %v", savedExp)
    44  	}
    45  	if called != 2 {
    46  		t.Errorf("expected called = 2, got called = %d", called)
    47  	}
    48  }
    49  
    50  func TestSubscriptionsUnsubscribe(t *testing.T) {
    51  	context := NewContext().WithAttributes(Attributes{"id": "1"})
    52  	gb := New(context)
    53  	exp1 := NewExperiment("experiment-1").WithVariations("result1", "result2")
    54  
    55  	var savedExp *Experiment
    56  	called := 0
    57  	unsubscribe := gb.Subscribe(func(exp *Experiment, result *Result) {
    58  		savedExp = exp
    59  		called++
    60  	})
    61  
    62  	gb.Run(exp1)
    63  	gb.WithAttributes(Attributes{"id": "3"})
    64  	unsubscribe()
    65  	gb.Run(exp1)
    66  
    67  	if !reflect.DeepEqual(savedExp, exp1) {
    68  		t.Errorf("unexpected experiment value: %v", savedExp)
    69  	}
    70  	if called != 1 {
    71  		t.Errorf("expected called = 1, got called = %d", called)
    72  	}
    73  }
    74  
    75  func TestSubscriptionsTrack(t *testing.T) {
    76  	context := NewContext().WithAttributes(Attributes{"id": "1"})
    77  	gb := New(context)
    78  	exp1 := NewExperiment("experiment-1").WithVariations("result1", "result2")
    79  	exp2 := NewExperiment("experiment-2").WithVariations("result3", "result4")
    80  
    81  	called := 0
    82  	context.WithTrackingCallback(func(exp *Experiment, result *Result) {
    83  		called++
    84  	})
    85  
    86  	gb.Run(exp1)
    87  	gb.Run(exp2)
    88  	gb.Run(exp1)
    89  	gb.Run(exp2)
    90  	gb.WithAttributes(Attributes{"id": "3"})
    91  	gb.Run(exp1)
    92  	gb.Run(exp2)
    93  	gb.Run(exp1)
    94  	gb.Run(exp2)
    95  	if called != 4 {
    96  		t.Errorf("expected called = 4, got called = %d", called)
    97  	}
    98  }
    99  
   100  func TestSubscriptionsRetrieve(t *testing.T) {
   101  	context := NewContext().WithAttributes(Attributes{"id": "1"})
   102  	gb := New(context)
   103  	exp1 := NewExperiment("experiment-1").WithVariations("result1", "result2")
   104  	exp2 := NewExperiment("experiment-2").WithVariations("result3", "result4")
   105  
   106  	gb.Run(exp1)
   107  	gb.Run(exp2)
   108  	resultsLen := len(gb.GetAllResults())
   109  	if resultsLen != 2 {
   110  		t.Errorf("expected results length = 2, got length = %d", resultsLen)
   111  	}
   112  }
   113  

View as plain text