|
| 1 | +package redis_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/bsm/ginkgo/v2" |
| 8 | + . "github.com/bsm/gomega" |
| 9 | + "github.com/redis/go-redis/v9" |
| 10 | +) |
| 11 | + |
| 12 | +func libCode(libName string) string { |
| 13 | + return fmt.Sprintf("#!js api_version=1.0 name=%s\n redis.registerFunction('foo', ()=>{{return 'bar'}})", libName) |
| 14 | +} |
| 15 | + |
| 16 | +func libCodeWithConfig(libName string) string { |
| 17 | + lib := `#!js api_version=1.0 name=%s |
| 18 | +
|
| 19 | + var last_update_field_name = "__last_update__" |
| 20 | + |
| 21 | + if (redis.config.last_update_field_name !== undefined) { |
| 22 | + if (typeof redis.config.last_update_field_name != 'string') { |
| 23 | + throw "last_update_field_name must be a string"; |
| 24 | + } |
| 25 | + last_update_field_name = redis.config.last_update_field_name |
| 26 | + } |
| 27 | + |
| 28 | + redis.registerFunction("hset", function(client, key, field, val){ |
| 29 | + // get the current time in ms |
| 30 | + var curr_time = client.call("time")[0]; |
| 31 | + return client.call('hset', key, field, val, last_update_field_name, curr_time); |
| 32 | + });` |
| 33 | + return fmt.Sprintf(lib, libName) |
| 34 | +} |
| 35 | + |
| 36 | +var _ = Describe("RedisGears commands", Label("gears"), func() { |
| 37 | + ctx := context.TODO() |
| 38 | + var client *redis.Client |
| 39 | + |
| 40 | + BeforeEach(func() { |
| 41 | + client = redis.NewClient(&redis.Options{Addr: ":6379"}) |
| 42 | + Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred()) |
| 43 | + }) |
| 44 | + |
| 45 | + AfterEach(func() { |
| 46 | + Expect(client.Close()).NotTo(HaveOccurred()) |
| 47 | + }) |
| 48 | + |
| 49 | + It("should TFunctionLoad, TFunctionLoadArgs and TFunctionDelete ", Label("gears", "tfunctionload"), func() { |
| 50 | + |
| 51 | + resultAdd, err := client.TFunctionLoad(ctx, libCode("libtflo1")).Result() |
| 52 | + Expect(err).NotTo(HaveOccurred()) |
| 53 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 54 | + opt := &redis.TFunctionLoadOptions{Replace: true, Config: `{"last_update_field_name":"last_update"}`} |
| 55 | + resultAdd, err = client.TFunctionLoadArgs(ctx, libCodeWithConfig("libtflo1"), opt).Result() |
| 56 | + Expect(err).NotTo(HaveOccurred()) |
| 57 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 58 | + resultAdd, err = client.TFunctionDelete(ctx, "libtflo1").Result() |
| 59 | + Expect(err).NotTo(HaveOccurred()) |
| 60 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 61 | + |
| 62 | + }) |
| 63 | + It("should TFunctionList", Label("gears", "tfunctionlist"), func() { |
| 64 | + resultAdd, err := client.TFunctionLoad(ctx, libCode("libtfli1")).Result() |
| 65 | + Expect(err).NotTo(HaveOccurred()) |
| 66 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 67 | + resultAdd, err = client.TFunctionLoad(ctx, libCode("libtfli2")).Result() |
| 68 | + Expect(err).NotTo(HaveOccurred()) |
| 69 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 70 | + resultList, err := client.TFunctionList(ctx).Result() |
| 71 | + Expect(err).NotTo(HaveOccurred()) |
| 72 | + Expect(resultList[0]["engine"]).To(BeEquivalentTo("js")) |
| 73 | + opt := &redis.TFunctionListOptions{Withcode: true, Verbose: 2} |
| 74 | + resultListArgs, err := client.TFunctionListArgs(ctx, opt).Result() |
| 75 | + Expect(err).NotTo(HaveOccurred()) |
| 76 | + Expect(resultListArgs[0]["code"]).To(BeEquivalentTo(libCode("libtfli1"))) |
| 77 | + resultAdd, err = client.TFunctionDelete(ctx, "libtfli1").Result() |
| 78 | + Expect(err).NotTo(HaveOccurred()) |
| 79 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 80 | + resultAdd, err = client.TFunctionDelete(ctx, "libtfli2").Result() |
| 81 | + Expect(err).NotTo(HaveOccurred()) |
| 82 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 83 | + }) |
| 84 | + |
| 85 | + It("should TFCall", Label("gears", "tfcall"), func() { |
| 86 | + var resultAdd interface{} |
| 87 | + resultAdd, err := client.TFunctionLoad(ctx, libCode("libtfc1")).Result() |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 90 | + resultAdd, err = client.TFCall(ctx, "libtfc1", "foo", 0).Result() |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + Expect(resultAdd).To(BeEquivalentTo("bar")) |
| 93 | + resultAdd, err = client.TFunctionDelete(ctx, "libtfc1").Result() |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 96 | + }) |
| 97 | + |
| 98 | + It("should TFCallArgs", Label("gears", "tfcallargs"), func() { |
| 99 | + var resultAdd interface{} |
| 100 | + resultAdd, err := client.TFunctionLoad(ctx, libCode("libtfca1")).Result() |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 103 | + opt := &redis.TFCallOptions{Arguments: []string{"foo", "bar"}} |
| 104 | + resultAdd, err = client.TFCallArgs(ctx, "libtfca1", "foo", 0, opt).Result() |
| 105 | + Expect(err).NotTo(HaveOccurred()) |
| 106 | + Expect(resultAdd).To(BeEquivalentTo("bar")) |
| 107 | + resultAdd, err = client.TFunctionDelete(ctx, "libtfca1").Result() |
| 108 | + Expect(err).NotTo(HaveOccurred()) |
| 109 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 110 | + }) |
| 111 | + |
| 112 | + It("should TFCallASYNC", Label("gears", "TFCallASYNC"), func() { |
| 113 | + var resultAdd interface{} |
| 114 | + resultAdd, err := client.TFunctionLoad(ctx, libCode("libtfc1")).Result() |
| 115 | + Expect(err).NotTo(HaveOccurred()) |
| 116 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 117 | + resultAdd, err = client.TFCallASYNC(ctx, "libtfc1", "foo", 0).Result() |
| 118 | + Expect(err).NotTo(HaveOccurred()) |
| 119 | + Expect(resultAdd).To(BeEquivalentTo("bar")) |
| 120 | + resultAdd, err = client.TFunctionDelete(ctx, "libtfc1").Result() |
| 121 | + Expect(err).NotTo(HaveOccurred()) |
| 122 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 123 | + }) |
| 124 | + |
| 125 | + It("should TFCallASYNCArgs", Label("gears", "TFCallASYNCargs"), func() { |
| 126 | + var resultAdd interface{} |
| 127 | + resultAdd, err := client.TFunctionLoad(ctx, libCode("libtfca1")).Result() |
| 128 | + Expect(err).NotTo(HaveOccurred()) |
| 129 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 130 | + opt := &redis.TFCallOptions{Arguments: []string{"foo", "bar"}} |
| 131 | + resultAdd, err = client.TFCallASYNCArgs(ctx, "libtfca1", "foo", 0, opt).Result() |
| 132 | + Expect(err).NotTo(HaveOccurred()) |
| 133 | + Expect(resultAdd).To(BeEquivalentTo("bar")) |
| 134 | + resultAdd, err = client.TFunctionDelete(ctx, "libtfca1").Result() |
| 135 | + Expect(err).NotTo(HaveOccurred()) |
| 136 | + Expect(resultAdd).To(BeEquivalentTo("OK")) |
| 137 | + }) |
| 138 | + |
| 139 | +}) |
0 commit comments