github.com/songzhibin97/gkit@v1.2.13/distributed/backend/backend_db/db_test.go (about)

     1  package backend_db
     2  
     3  import (
     4  	"database/sql"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/songzhibin97/gkit/generator"
    10  
    11  	"github.com/songzhibin97/gkit/distributed/task"
    12  	"github.com/stretchr/testify/assert"
    13  	"gorm.io/gorm"
    14  
    15  	"github.com/songzhibin97/gkit/distributed/backend"
    16  )
    17  
    18  func InitBackend() backend.Backend {
    19  	dsn := "root:123456@tcp(127.0.0.1:3306)/gkit?charset=utf8mb4&parseTime=True&loc=Local"
    20  	sqlDB, err := sql.Open("mysql", dsn)
    21  	if err != nil {
    22  		return nil
    23  	}
    24  	return NewBackendSQLDB(sqlDB, -1, "mysql", nil)
    25  }
    26  
    27  func TestGroupTaskOver(t *testing.T) {
    28  	_backend := InitBackend()
    29  	if _backend == nil {
    30  		t.Skip()
    31  	}
    32  	g := generator.NewSnowflake(time.Now().Local(), 1)
    33  	ids := make([]string, 0, 3)
    34  	for i := 0; i < 3; i++ {
    35  		id, _ := g.NextID()
    36  		if i == 0 {
    37  			ids = append(ids, "group:"+strconv.FormatUint(id, 10))
    38  		} else {
    39  			ids = append(ids, "task:"+strconv.FormatUint(id, 10))
    40  		}
    41  
    42  	}
    43  	var (
    44  		group = task.GroupMeta{
    45  			GroupID: ids[0],
    46  			Name:    "group",
    47  		}
    48  		task1 = task.Signature{
    49  			ID:      ids[1],
    50  			GroupID: group.GroupID,
    51  			Name:    "task1",
    52  		}
    53  		task2 = task.Signature{
    54  			ID:      ids[2],
    55  			GroupID: group.GroupID,
    56  			Name:    "task2",
    57  		}
    58  	)
    59  	_ = _backend.ResetGroup(group.GroupID)
    60  	_ = _backend.ResetTask(task1.ID, task2.ID)
    61  	isCompleted, err := _backend.GroupCompleted(group.GroupID)
    62  	if assert.Error(t, err) {
    63  		assert.False(t, isCompleted)
    64  		assert.Error(t, err, gorm.ErrNotImplemented)
    65  	}
    66  	_ = _backend.GroupTakeOver(group.GroupID, group.Name, task1.ID, task2.ID)
    67  	isCompleted, err = _backend.GroupCompleted(group.GroupID)
    68  	if assert.NoError(t, err) {
    69  		assert.False(t, isCompleted)
    70  	}
    71  
    72  	_ = _backend.SetStatePending(&task1)
    73  	_ = _backend.SetStateStarted(&task2)
    74  	isCompleted, err = _backend.GroupCompleted(group.GroupID)
    75  	if assert.NoError(t, err) {
    76  		assert.False(t, isCompleted)
    77  	}
    78  	result := []*task.Result{{
    79  		Type:  "int",
    80  		Value: 1,
    81  	}}
    82  	_ = _backend.SetStateStarted(&task1)
    83  	_ = _backend.SetStateSuccess(&task2, result)
    84  	isCompleted, err = _backend.GroupCompleted(group.GroupID)
    85  	if assert.NoError(t, err) {
    86  		assert.False(t, isCompleted)
    87  	}
    88  	_ = _backend.SetStateFailure(&task1, "failure")
    89  	isCompleted, err = _backend.GroupCompleted(group.GroupID)
    90  	if assert.NoError(t, err) {
    91  		assert.True(t, isCompleted)
    92  	}
    93  }
    94  
    95  func TestGetStatus(t *testing.T) {
    96  	_backend := InitBackend()
    97  	if _backend == nil {
    98  		t.Skip()
    99  	}
   100  	task1 := task.Signature{
   101  		ID:      "task1",
   102  		GroupID: "group",
   103  		Name:    "task1",
   104  	}
   105  	_ = _backend.ResetTask(task1.ID)
   106  
   107  	status, err := _backend.GetStatus(task1.ID)
   108  	assert.Equal(t, err, gorm.ErrRecordNotFound)
   109  	assert.Nil(t, status)
   110  
   111  	_ = _backend.SetStatePending(&task1)
   112  	status, err = _backend.GetStatus(task1.ID)
   113  	assert.NoError(t, err)
   114  	assert.Equal(t, status.Status, task.StatePending)
   115  
   116  	_ = _backend.SetStateReceived(&task1)
   117  	status, err = _backend.GetStatus(task1.ID)
   118  	assert.NoError(t, err)
   119  	assert.Equal(t, status.Status, task.StateReceived)
   120  
   121  	_ = _backend.SetStateStarted(&task1)
   122  	status, err = _backend.GetStatus(task1.ID)
   123  	assert.NoError(t, err)
   124  	assert.Equal(t, status.Status, task.StateStarted)
   125  
   126  	result := &task.Result{
   127  		Type:  "int",
   128  		Value: 1,
   129  	}
   130  	_ = _backend.SetStateSuccess(&task1, []*task.Result{result})
   131  	status, err = _backend.GetStatus(task1.ID)
   132  	assert.NoError(t, err)
   133  	assert.Equal(t, status.Status, task.StateSuccess)
   134  }
   135  
   136  func TestResult(t *testing.T) {
   137  	_backend := InitBackend()
   138  	if _backend == nil {
   139  		t.Skip()
   140  	}
   141  	task1 := task.Signature{
   142  		ID:      "task1",
   143  		GroupID: "group",
   144  		Name:    "task1",
   145  	}
   146  	_ = _backend.SetStatePending(&task1)
   147  	status, err := _backend.GetStatus(task1.ID)
   148  	assert.NoError(t, err)
   149  	assert.Equal(t, status.Status, task.StatePending)
   150  
   151  	_ = _backend.ResetTask(task1.ID)
   152  
   153  	status, err = _backend.GetStatus(task1.ID)
   154  	assert.Equal(t, err, gorm.ErrRecordNotFound)
   155  	assert.Nil(t, status)
   156  }