github.com/Laplace-Game-Development/Laplace-Entangled-Environment@v0.0.3/internal/schedule/task_test.go (about)

     1  package schedule
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/redis"
     8  	"github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/startup"
     9  	"github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/util"
    10  	"github.com/Laplace-Game-Development/Laplace-Entangled-Environment/internal/zeromq"
    11  	"github.com/mediocregopher/radix/v3"
    12  )
    13  
    14  //// Configurables
    15  const unitTestTableName string = "unitTestSet"
    16  const values int = 20
    17  const waitTime time.Duration = time.Second * 5
    18  
    19  func TestTask(t *testing.T) {
    20  	cleanup := startup.InitServerStartupOnTaskList(
    21  		[]startup.ServerTask{
    22  			redis.StartDatabase,
    23  			zeromq.StartZeroMqComms,
    24  			StartTaskQueue,
    25  		})
    26  	defer cleanup()
    27  
    28  	t.Run("UnitTestTask", testUnitTestTask)
    29  }
    30  
    31  func testUnitTestTask(t *testing.T) {
    32  	t.Log("Starting UnitTestTask! \n")
    33  	msgs := make([]string, values)
    34  	set := map[string]bool{}
    35  	var temp string
    36  	for i := 0; i < values; i++ {
    37  		temp = util.RandStringN(128)
    38  		msgs[i] = constructTaskWithPrefix(TestTaskPrefix, unitTestTableName, temp)
    39  		set[temp] = false
    40  	}
    41  
    42  	// Delete Key In case of previous failed run attempts
    43  	redis.MainRedis.Do(radix.Cmd(nil, "DEL", unitTestTableName))
    44  
    45  	SendTasksToWorkers(msgs...)
    46  
    47  	// No easy way to tell when work is done so just sleep
    48  	time.Sleep(waitTime)
    49  
    50  	var uniqueMsgs []string
    51  	redis.MainRedis.Do(radix.Cmd(&uniqueMsgs, "SMEMBERS", unitTestTableName))
    52  	t.Logf("Got Members! Msgs: %v\n", uniqueMsgs)
    53  
    54  	var value bool
    55  	var exists bool
    56  	for _, msg := range uniqueMsgs {
    57  		value, exists = set[msg]
    58  		if !exists || value {
    59  			t.Errorf("Had Erroneous value in Set! Msg: %v\n", msg)
    60  		}
    61  
    62  		set[msg] = true
    63  	}
    64  
    65  	actual := len(set)
    66  	var expected int
    67  	redis.MainRedis.Do(radix.Cmd(&expected, "SCARD", unitTestTableName))
    68  	if actual != expected {
    69  		t.Error("Did not get all keys expected for Task Working... Consider Increasing Sleep Period!")
    70  	}
    71  
    72  	// Delete Key When Finished
    73  	redis.MainRedis.Do(radix.Cmd(nil, "DEL", unitTestTableName))
    74  }