vitess.io/vitess@v0.16.2/go/vt/servenv/servenv_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     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  
    17  package servenv
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"vitess.io/vitess/go/event"
    24  )
    25  
    26  func TestFireOnTermSyncHooksFinished(t *testing.T) {
    27  	onTermSyncHooks = event.Hooks{}
    28  
    29  	triggered1 := false
    30  	OnTermSync(func() {
    31  		triggered1 = true
    32  	})
    33  	triggered2 := false
    34  	OnTermSync(func() {
    35  		triggered2 = true
    36  	})
    37  
    38  	if finished, want := fireOnTermSyncHooks(1*time.Second), true; finished != want {
    39  		t.Errorf("finished = %v, want %v", finished, want)
    40  	}
    41  	if want := true; triggered1 != want {
    42  		t.Errorf("triggered1 = %v, want %v", triggered1, want)
    43  	}
    44  	if want := true; triggered2 != want {
    45  		t.Errorf("triggered1 = %v, want %v", triggered2, want)
    46  	}
    47  }
    48  
    49  func TestFireOnTermSyncHooksTimeout(t *testing.T) {
    50  	onTermSyncHooks = event.Hooks{}
    51  
    52  	OnTermSync(func() {
    53  		time.Sleep(1 * time.Second)
    54  	})
    55  
    56  	if finished, want := fireOnTermSyncHooks(1*time.Nanosecond), false; finished != want {
    57  		t.Errorf("finished = %v, want %v", finished, want)
    58  	}
    59  }
    60  
    61  func TestFireOnCloseHooksTimeout(t *testing.T) {
    62  	onCloseHooks = event.Hooks{}
    63  
    64  	OnClose(func() {
    65  		time.Sleep(1 * time.Second)
    66  	})
    67  
    68  	if finished, want := fireOnCloseHooks(1*time.Nanosecond), false; finished != want {
    69  		t.Errorf("finished = %v, want %v", finished, want)
    70  	}
    71  }