script_test.go 2.5 KB

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