github.com/kaydxh/golang@v0.0.131/pkg/binlog/binlog_test.go (about)

     1  /*
     2   *Copyright (c) 2023, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  
    23  package binlog_test
    24  
    25  import (
    26  	"context"
    27  	"database/sql"
    28  	"testing"
    29  
    30  	"encoding/json"
    31  
    32  	"github.com/google/uuid"
    33  	"github.com/kaydxh/golang/pkg/binlog"
    34  	binlog_ "github.com/kaydxh/golang/pkg/binlog"
    35  	ds_ "github.com/kaydxh/golang/pkg/binlog/datastore"
    36  	mysql_ "github.com/kaydxh/golang/pkg/database/mysql"
    37  	mq_ "github.com/kaydxh/golang/pkg/mq"
    38  	kafka_ "github.com/kaydxh/golang/pkg/mq/kafka"
    39  	viper_ "github.com/kaydxh/golang/pkg/viper"
    40  	"github.com/segmentio/kafka-go"
    41  )
    42  
    43  type TaskTable struct {
    44  	//	Id sql.NullInt64 `db:"id"` // primary key ID
    45  
    46  	// NullTime represents a time.Time that may be null.
    47  	// NullTime implements the Scanner interface so
    48  	// it can be used as a scan destination, similar to NullString.
    49  	CreateTime sql.NullTime `db:"create_time"`
    50  	UpdateTime sql.NullTime `db:"update_time"`
    51  
    52  	GroupId    string `db:"group_id"`
    53  	PageId     string `db:"page_id"`
    54  	FeaId      string `db:"fea_id"`
    55  	EntityId   string `db:"entity_id"`
    56  	Feature0   []byte `db:"feature0"`
    57  	Feature1   []byte `db:"feature1"`
    58  	ExtendInfo []byte `db:"extend_info"`
    59  }
    60  
    61  func TestProducer(t *testing.T) {
    62  	cfgFile := "./binlog.yaml"
    63  	config := kafka_.NewConfig(kafka_.WithViper(viper_.GetViper(cfgFile, "mq.kafka")))
    64  
    65  	mq, err := config.Complete().New(context.Background())
    66  	if err != nil {
    67  		t.Errorf("failed to new config err: %v", err)
    68  	}
    69  
    70  	topic := "topic-test-1"
    71  	ctx := context.Background()
    72  	ps, err := mq.AsProducers(ctx, topic)
    73  	if err != nil {
    74  		t.Fatalf("failed to as producers, err: %v", err)
    75  	}
    76  	if len(ps) == 0 {
    77  		t.Fatalf("producers nil")
    78  	}
    79  	p := ps[0]
    80  
    81  	tableName := "hetu_zeus_0"
    82  	cols := []string{"group_id", "page_id", "fea_id", "entity_id", "feature0", "feature1", "extend_info"}
    83  	msgKey := &ds_.MessageKey{
    84  		Key:     "Key-A",
    85  		MsgType: ds_.MsgType_Insert,
    86  		Fields:  cols,
    87  		Path:    tableName,
    88  	}
    89  	keyData, _ := json.Marshal(msgKey)
    90  
    91  	for i := 0; i < 10; i++ {
    92  		arg := &TaskTable{
    93  			GroupId:    "groupId-1",
    94  			PageId:     "100",
    95  			FeaId:      uuid.NewString(),
    96  			Feature0:   []byte("Feature0"),
    97  			Feature1:   []byte("Feature1"),
    98  			ExtendInfo: []byte("ExtendInfo"),
    99  		}
   100  		msgValue, _ := json.Marshal(arg)
   101  		err = p.Send(ctx,
   102  			kafka.Message{
   103  				Key:   keyData,
   104  				Value: msgValue,
   105  			},
   106  		)
   107  		if err != nil {
   108  			t.Fatalf("failed to send messages, err: %v", err)
   109  		}
   110  	}
   111  
   112  }
   113  
   114  func TestNewBinlog(t *testing.T) {
   115  	// install kafka
   116  	cfgFile := "./binlog.yaml"
   117  	config := kafka_.NewConfig(kafka_.WithViper(viper_.GetViper(cfgFile, "mq.kafka")))
   118  
   119  	mq, err := config.Complete().New(context.Background())
   120  	if err != nil {
   121  		t.Errorf("failed to new config err: %v", err)
   122  	}
   123  
   124  	ctx := context.Background()
   125  	topic := "topic-test-1"
   126  	_, err = mq.AsConsumers(ctx, topic)
   127  	if err != nil {
   128  		t.Fatalf("failed to as producers, err: %v", err)
   129  	}
   130  	consumer, err := mq.GetConsumer(topic)
   131  	if err != nil {
   132  		t.Fatalf("failed to get producer, err: %v", err)
   133  	}
   134  
   135  	dbConfig := mysql_.NewConfig(mysql_.WithViper(viper_.GetViper(cfgFile, "database.mysql")))
   136  
   137  	db, err := dbConfig.Complete().New(context.Background())
   138  	if err != nil {
   139  		panic(err)
   140  	}
   141  	if db == nil {
   142  		panic("db is not enable")
   143  	}
   144  
   145  	bsConfig := binlog_.NewConfig(binlog_.WithViper(viper_.GetViper(cfgFile, "binlog")))
   146  
   147  	bs, err := bsConfig.Complete().New(context.Background(), nil, []mq_.Consumer{consumer},
   148  		binlog.WithMessageDecoderFunc(func(ctx context.Context, data []byte) (interface{}, error) {
   149  
   150  			var arg TaskTable
   151  			err = json.Unmarshal(data, &arg)
   152  			if err != nil {
   153  				return arg, err
   154  			}
   155  			return arg, nil
   156  
   157  		}),
   158  
   159  		binlog_.WithMessageKeyDecodeFunc(func(ctx context.Context, data []byte) (ds_.MessageKey, error) {
   160  			var msgKey ds_.MessageKey
   161  			err := json.Unmarshal(data, &msgKey)
   162  			return msgKey, err
   163  
   164  		}))
   165  	if err != nil {
   166  		panic(err)
   167  	}
   168  	_ = bs
   169  
   170  	select {}
   171  }