script_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis_test
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/garyburd/redigo/internal/redistest"
  21. "github.com/garyburd/redigo/redis"
  22. )
  23. func ExampleScript(c redis.Conn, reply interface{}, err error) {
  24. // Initialize a package-level variable with a script.
  25. var getScript = redis.NewScript(1, `return redis.call('get', KEYS[1])`)
  26. // In a function, use the script Do method to evaluate the script. The Do
  27. // method optimistically uses the EVALSHA command. If the script is not
  28. // loaded, then the Do method falls back to the EVAL command.
  29. reply, err = getScript.Do(c, "foo")
  30. }
  31. func TestScript(t *testing.T) {
  32. c, err := redistest.Dial()
  33. if err != nil {
  34. t.Fatalf("error connection to database, %v", err)
  35. }
  36. defer c.Close()
  37. // To test fall back in Do, we make script unique by adding comment with current time.
  38. script := fmt.Sprintf("--%d\nreturn {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", time.Now().UnixNano())
  39. s := redis.NewScript(2, script)
  40. reply := []interface{}{[]byte("key1"), []byte("key2"), []byte("arg1"), []byte("arg2")}
  41. v, err := s.Do(c, "key1", "key2", "arg1", "arg2")
  42. if err != nil {
  43. t.Errorf("s.Do(c, ...) returned %v", err)
  44. }
  45. if !reflect.DeepEqual(v, reply) {
  46. t.Errorf("s.Do(c, ..); = %v, want %v", v, reply)
  47. }
  48. err = s.Load(c)
  49. if err != nil {
  50. t.Errorf("s.Load(c) returned %v", err)
  51. }
  52. err = s.SendHash(c, "key1", "key2", "arg1", "arg2")
  53. if err != nil {
  54. t.Errorf("s.SendHash(c, ...) returned %v", err)
  55. }
  56. err = c.Flush()
  57. if err != nil {
  58. t.Errorf("c.Flush() returned %v", err)
  59. }
  60. v, err = c.Receive()
  61. if !reflect.DeepEqual(v, reply) {
  62. t.Errorf("s.SendHash(c, ..); c.Receive() = %v, want %v", v, reply)
  63. }
  64. err = s.Send(c, "key1", "key2", "arg1", "arg2")
  65. if err != nil {
  66. t.Errorf("s.Send(c, ...) returned %v", err)
  67. }
  68. err = c.Flush()
  69. if err != nil {
  70. t.Errorf("c.Flush() returned %v", err)
  71. }
  72. v, err = c.Receive()
  73. if !reflect.DeepEqual(v, reply) {
  74. t.Errorf("s.Send(c, ..); c.Receive() = %v, want %v", v, reply)
  75. }
  76. }