vitess.io/vitess@v0.16.2/go/vt/vttablet/endtoend/healthstream_test.go (about)

     1  /*
     2  Copyright 2021 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 endtoend
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"vitess.io/vitess/go/test/utils"
    26  	querypb "vitess.io/vitess/go/vt/proto/query"
    27  	"vitess.io/vitess/go/vt/vttablet/endtoend/framework"
    28  )
    29  
    30  func TestSchemaChange(t *testing.T) {
    31  	client := framework.NewClient()
    32  
    33  	tcs := []struct {
    34  		tName    string
    35  		response []string
    36  		ddl      string
    37  	}{
    38  		{
    39  			"create table 1",
    40  			[]string{"vitess_sc1"},
    41  			"create table vitess_sc1(id bigint primary key)",
    42  		}, {
    43  			"create table 2",
    44  			[]string{"vitess_sc2"},
    45  			"create table vitess_sc2(id bigint primary key)",
    46  		}, {
    47  			"add column 1",
    48  			[]string{"vitess_sc1"},
    49  			"alter table vitess_sc1 add column newCol varchar(50)",
    50  		}, {
    51  			"add column 2",
    52  			[]string{"vitess_sc2"},
    53  			"alter table vitess_sc2 add column newCol varchar(50)",
    54  		}, {
    55  			"remove column",
    56  			[]string{"vitess_sc1"},
    57  			"alter table vitess_sc1 drop column newCol",
    58  		}, {
    59  			"drop table 2",
    60  			[]string{"vitess_sc2"},
    61  			"drop table vitess_sc2",
    62  		}, {
    63  			"drop table 1",
    64  			[]string{"vitess_sc1"},
    65  			"drop table vitess_sc1",
    66  		},
    67  	}
    68  
    69  	ch := make(chan []string, 100)
    70  	go func(ch chan []string) {
    71  		client.StreamHealth(func(response *querypb.StreamHealthResponse) error {
    72  			if response.RealtimeStats.TableSchemaChanged != nil {
    73  				ch <- response.RealtimeStats.TableSchemaChanged
    74  			}
    75  			return nil
    76  		})
    77  	}(ch)
    78  
    79  	select {
    80  	case <-ch: // get the schema notification
    81  	case <-time.After(3 * time.Second):
    82  		// We might not see the initial changes
    83  		// as the health stream ticker would have started very early on and
    84  		// this test client might not be even registered.
    85  	}
    86  
    87  	for _, tc := range tcs {
    88  		t.Run(tc.tName, func(t *testing.T) {
    89  			_, err := client.Execute(tc.ddl, nil)
    90  			assert.NoError(t, err)
    91  			select {
    92  			case res := <-ch: // get the schema notification
    93  				utils.MustMatch(t, tc.response, res, "")
    94  			case <-time.After(5 * time.Second):
    95  				t.Errorf("timed out")
    96  				return
    97  			}
    98  		})
    99  	}
   100  }