github.com/anycable/anycable-go@v1.5.1/node/disconnect_queue_test.go (about) 1 package node 2 3 import ( 4 "context" 5 "log/slog" 6 "runtime" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestDisconnectQueue_Run(t *testing.T) { 13 t.Run("Disconnects sessions", func(t *testing.T) { 14 ctx := context.Background() 15 q := newQueue() 16 defer q.Shutdown(ctx) //nolint:errcheck 17 18 assert.Nil(t, q.Enqueue(NewMockSession("1", q.node))) 19 assert.Equal(t, 1, q.Size()) 20 21 go func() { 22 if err := q.Run(); err != nil { 23 panic(err) 24 } 25 }() 26 27 // TODO: We need a trully node mock to control disconnect operation 28 for q.Size() > 0 { 29 runtime.Gosched() 30 } 31 }) 32 } 33 34 func TestDisconnectQueue_Shutdown(t *testing.T) { 35 t.Run("Disconnects sessions", func(t *testing.T) { 36 ctx := context.Background() 37 q := newQueue() 38 39 assert.Nil(t, q.Enqueue(NewMockSession("1", q.node))) 40 assert.Nil(t, q.Enqueue(NewMockSession("2", q.node))) 41 assert.Equal(t, 2, q.Size()) 42 43 assert.Nil(t, q.Shutdown(ctx)) 44 assert.Equal(t, 0, q.Size()) 45 }) 46 47 t.Run("Stops after timeout", func(t *testing.T) { 48 t.Skip("TODO: We need a trully node mock to control disconnect operation") 49 }) 50 51 t.Run("Allows multiple entering", func(t *testing.T) { 52 q := newQueue() 53 ctx := context.Background() 54 55 for i := 1; i <= 10; i++ { 56 q.Shutdown(ctx) // nolint:errcheck 57 } 58 }) 59 } 60 61 func TestDisconnectQueue_Enqueue(t *testing.T) { 62 t.Run("Adds sessions to the queue", func(t *testing.T) { 63 q := newQueue() 64 65 assert.Nil(t, q.Enqueue(NewMockSession("1", q.node))) 66 assert.Equal(t, 1, q.Size()) 67 }) 68 69 t.Run("After shutdown", func(t *testing.T) { 70 ctx := context.Background() 71 q := newQueue() 72 q.Shutdown(ctx) // nolint:errcheck 73 74 assert.Nil(t, q.Enqueue(NewMockSession("1", q.node))) 75 assert.Equal(t, 0, q.Size()) 76 }) 77 } 78 79 func newQueue() *DisconnectQueue { 80 node := NewMockNode() 81 config := NewDisconnectQueueConfig() 82 config.Rate = 1 83 q := NewDisconnectQueue(node, &config, slog.Default()) 84 85 return q 86 }