You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

101 lines
2.6 KiB

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