github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/callback_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  //     http://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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package dbs
    15  
    16  import (
    17  	"context"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/log"
    21  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    22  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    23  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    24  	"go.uber.org/zap"
    25  )
    26  
    27  type TestInterceptor struct {
    28  	*BaseInterceptor
    29  
    30  	OnGetSchemaReplicantExported func(ctx stochastikctx.Context, is schemareplicant.SchemaReplicant) schemareplicant.SchemaReplicant
    31  }
    32  
    33  func (ti *TestInterceptor) OnGetSchemaReplicant(ctx stochastikctx.Context, is schemareplicant.SchemaReplicant) schemareplicant.SchemaReplicant {
    34  	if ti.OnGetSchemaReplicantExported != nil {
    35  		return ti.OnGetSchemaReplicantExported(ctx, is)
    36  	}
    37  
    38  	return ti.BaseInterceptor.OnGetSchemaReplicant(ctx, is)
    39  }
    40  
    41  type TestDBSCallback struct {
    42  	*BaseCallback
    43  
    44  	onJobRunBefore         func(*perceptron.Job)
    45  	OnJobRunBeforeExported func(*perceptron.Job)
    46  	onJobUFIDelated           func(*perceptron.Job)
    47  	OnJobUFIDelatedExported   func(*perceptron.Job)
    48  	onWatched              func(ctx context.Context)
    49  }
    50  
    51  func (tc *TestDBSCallback) OnJobRunBefore(job *perceptron.Job) {
    52  	log.Info("on job run before", zap.String("job", job.String()))
    53  	if tc.OnJobRunBeforeExported != nil {
    54  		tc.OnJobRunBeforeExported(job)
    55  		return
    56  	}
    57  	if tc.onJobRunBefore != nil {
    58  		tc.onJobRunBefore(job)
    59  		return
    60  	}
    61  
    62  	tc.BaseCallback.OnJobRunBefore(job)
    63  }
    64  
    65  func (tc *TestDBSCallback) OnJobUFIDelated(job *perceptron.Job) {
    66  	log.Info("on job uFIDelated", zap.String("job", job.String()))
    67  	if tc.OnJobUFIDelatedExported != nil {
    68  		tc.OnJobUFIDelatedExported(job)
    69  		return
    70  	}
    71  	if tc.onJobUFIDelated != nil {
    72  		tc.onJobUFIDelated(job)
    73  		return
    74  	}
    75  
    76  	tc.BaseCallback.OnJobUFIDelated(job)
    77  }
    78  
    79  func (tc *TestDBSCallback) OnWatched(ctx context.Context) {
    80  	if tc.onWatched != nil {
    81  		tc.onWatched(ctx)
    82  		return
    83  	}
    84  
    85  	tc.BaseCallback.OnWatched(ctx)
    86  }
    87  
    88  func (s *testDBSSuite) TestCallback(c *C) {
    89  	cb := &BaseCallback{}
    90  	c.Assert(cb.OnChanged(nil), IsNil)
    91  	cb.OnJobRunBefore(nil)
    92  	cb.OnJobUFIDelated(nil)
    93  	cb.OnWatched(context.TODO())
    94  }