script_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "github.com/garyburd/redigo/redis"
  18. "reflect"
  19. "testing"
  20. "time"
  21. )
  22. func TestScript(t *testing.T) {
  23. c, err := dial()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer c.Close()
  28. // To test fallback in Do, we make script unique by adding comment with current time.
  29. script := fmt.Sprintf("--%d\nreturn {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", time.Now().UnixNano())
  30. s := redis.NewScript(2, script)
  31. result := []interface{}{[]byte("key1"), []byte("key2"), []byte("arg1"), []byte("arg2")}
  32. v, err := s.Do(c, "key1", "key2", "arg1", "arg2")
  33. if err != nil {
  34. t.Errorf("s.Do(c, ...) returned %v", err)
  35. }
  36. if !reflect.DeepEqual(v, result) {
  37. t.Errorf("s.Do(c, ..); = %v, want %v", v, result)
  38. }
  39. err = s.Load(c)
  40. if err != nil {
  41. t.Errorf("s.Load(c) returned %v", err)
  42. }
  43. err = s.SendHash(c, "key1", "key2", "arg1", "arg2")
  44. if err != nil {
  45. t.Errorf("s.SendHash(c, ...) returned %v", err)
  46. }
  47. v, err = c.Receive()
  48. if !reflect.DeepEqual(v, result) {
  49. t.Errorf("s.SendHash(c, ..); s.Recevie() = %v, want %v", v, result)
  50. }
  51. err = s.Send(c, "key1", "key2", "arg1", "arg2")
  52. if err != nil {
  53. t.Errorf("s.Send(c, ...) returned %v", err)
  54. }
  55. v, err = c.Receive()
  56. if !reflect.DeepEqual(v, result) {
  57. t.Errorf("s.Send(c, ..); s.Recevie() = %v, want %v", v, result)
  58. }
  59. }