|
| 1 | +package arrow_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "github.com/tarantool/go-tarantool/v2" |
| 13 | + "github.com/tarantool/go-tarantool/v2/arrow" |
| 14 | + "github.com/tarantool/go-tarantool/v2/test_helpers" |
| 15 | +) |
| 16 | + |
| 17 | +var isArrowSupported = false |
| 18 | + |
| 19 | +var server = "127.0.0.1:3013" |
| 20 | +var dialer = tarantool.NetDialer{ |
| 21 | + Address: server, |
| 22 | + User: "test", |
| 23 | + Password: "test", |
| 24 | +} |
| 25 | +var space = "testArrow" |
| 26 | + |
| 27 | +var opts = tarantool.Opts{ |
| 28 | + Timeout: 5 * time.Second, |
| 29 | +} |
| 30 | + |
| 31 | +// TestInsert uses Arrow sequence from Tarantool's test . |
| 32 | +// See: https://github.com/tarantool/tarantool/blob/master/test/box-luatest/gh_10508_iproto_insert_arrow_test.lua |
| 33 | +func TestInsert_invalid(t *testing.T) { |
| 34 | + arrows := []struct { |
| 35 | + arrow string |
| 36 | + expected string |
| 37 | + }{ |
| 38 | + { |
| 39 | + "", |
| 40 | + "no Arrow data", |
| 41 | + }, |
| 42 | + { |
| 43 | + "00", |
| 44 | + "Failed to decode Arrow IPC data", |
| 45 | + }, |
| 46 | + { |
| 47 | + "ffffffff70000000040000009effffff0400010004000000" + |
| 48 | + "b6ffffff0c00000004000000000000000100000004000000daffffff140000000202" + |
| 49 | + "000004000000f0ffffff4000000001000000610000000600080004000c0010000400" + |
| 50 | + "080009000c000c000c0000000400000008000a000c00040006000800ffffffff8800" + |
| 51 | + "0000040000008affffff0400030010000000080000000000000000000000acffffff" + |
| 52 | + "01000000000000003400000008000000000000000200000000000000000000000000" + |
| 53 | + "00000000000000000000000000000800000000000000000000000100000001000000" + |
| 54 | + "0000000000000000000000000a00140004000c0010000c0014000400060008000c00" + |
| 55 | + "00000000000000000000", |
| 56 | + "memtx does not support arrow format", |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + conn := test_helpers.ConnectWithValidation(t, dialer, opts) |
| 61 | + defer conn.Close() |
| 62 | + |
| 63 | + for i, a := range arrows { |
| 64 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 65 | + data, err := hex.DecodeString(a.arrow) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + arr, err := arrow.MakeArrow(data) |
| 69 | + if err != nil { |
| 70 | + require.ErrorContains(t, err, a.expected) |
| 71 | + return |
| 72 | + } |
| 73 | + req := arrow.NewInsertRequest(space, arr) |
| 74 | + |
| 75 | + _, err = conn.Do(req).Get() |
| 76 | + require.ErrorContains(t, err, a.expected) |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +// runTestMain is a body of TestMain function |
| 83 | +// (see https://pkg.go.dev/testing#hdr-Main). |
| 84 | +// Using defer + os.Exit is not works so TestMain body |
| 85 | +// is a separate function, see |
| 86 | +// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls |
| 87 | +func runTestMain(m *testing.M) int { |
| 88 | + isLess, err := test_helpers.IsTarantoolVersionLess(3, 3, 0) |
| 89 | + if err != nil { |
| 90 | + log.Fatalf("Failed to extract Tarantool version: %s", err) |
| 91 | + } |
| 92 | + isArrowSupported = !isLess |
| 93 | + |
| 94 | + if !isArrowSupported { |
| 95 | + log.Println("Skipping insert Arrow tests...") |
| 96 | + return m.Run() |
| 97 | + } |
| 98 | + |
| 99 | + instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{ |
| 100 | + Dialer: dialer, |
| 101 | + InitScript: "config.lua", |
| 102 | + Listen: server, |
| 103 | + WaitStart: 100 * time.Millisecond, |
| 104 | + ConnectRetry: 10, |
| 105 | + RetryTimeout: 500 * time.Millisecond, |
| 106 | + }) |
| 107 | + defer test_helpers.StopTarantoolWithCleanup(instance) |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + log.Printf("Failed to prepare test Tarantool: %s", err) |
| 111 | + return 1 |
| 112 | + } |
| 113 | + |
| 114 | + return m.Run() |
| 115 | +} |
| 116 | + |
| 117 | +func TestMain(m *testing.M) { |
| 118 | + code := runTestMain(m) |
| 119 | + os.Exit(code) |
| 120 | +} |
0 commit comments