script_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. )
  21. func ExampleScript() {
  22. c, err := Dial("tcp", ":6379")
  23. if err != nil {
  24. // handle error
  25. }
  26. defer c.Close()
  27. // Initialize a package-level variable with a script.
  28. var getScript = NewScript(1, `return call('get', KEYS[1])`)
  29. // In a function, use the script Do method to evaluate the script. The Do
  30. // method optimistically uses the EVALSHA command. If the script is not
  31. // loaded, then the Do method falls back to the EVAL command.
  32. if _, err = getScript.Do(c, "foo"); err != nil {
  33. // handle error
  34. }
  35. }
  36. func TestScript(t *testing.T) {
  37. c, err := DialDefaultServer()
  38. if err != nil {
  39. t.Fatalf("error connection to database, %v", err)
  40. }
  41. defer c.Close()
  42. // To test fall back in Do, we make script unique by adding comment with current time.
  43. script := fmt.Sprintf("--%d\nreturn {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", time.Now().UnixNano())
  44. s := NewScript(2, script)
  45. reply := []interface{}{[]byte("key1"), []byte("key2"), []byte("arg1"), []byte("arg2")}
  46. v, err := s.Do(c, "key1", "key2", "arg1", "arg2")
  47. if err != nil {
  48. t.Errorf("s.Do(c, ...) returned %v", err)
  49. }
  50. if !reflect.DeepEqual(v, reply) {
  51. t.Errorf("s.Do(c, ..); = %v, want %v", v, reply)
  52. }
  53. err = s.Load(c)
  54. if err != nil {
  55. t.Errorf("s.Load(c) returned %v", err)
  56. }
  57. err = s.SendHash(c, "key1", "key2", "arg1", "arg2")
  58. if err != nil {
  59. t.Errorf("s.SendHash(c, ...) returned %v", err)
  60. }
  61. err = c.Flush()
  62. if err != nil {
  63. t.Errorf("c.Flush() returned %v", err)
  64. }
  65. v, err = c.Receive()
  66. if !reflect.DeepEqual(v, reply) {
  67. t.Errorf("s.SendHash(c, ..); c.Receive() = %v, want %v", v, reply)
  68. }
  69. err = s.Send(c, "key1", "key2", "arg1", "arg2")
  70. if err != nil {
  71. t.Errorf("s.Send(c, ...) returned %v", err)
  72. }
  73. err = c.Flush()
  74. if err != nil {
  75. t.Errorf("c.Flush() returned %v", err)
  76. }
  77. v, err = c.Receive()
  78. if !reflect.DeepEqual(v, reply) {
  79. t.Errorf("s.Send(c, ..); c.Receive() = %v, want %v", v, reply)
  80. }
  81. }