file_sink_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain 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,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bufio"
  17. "os"
  18. "reflect"
  19. "testing"
  20. "github.com/lf-edge/ekuiper/sdk/go/mock"
  21. )
  22. var CACHE_FILE = "cache"
  23. func TestFileSink(t *testing.T) {
  24. defer os.Remove(CACHE_FILE)
  25. s := &fileSink{}
  26. s.Configure(make(map[string]interface{}))
  27. exp := []string{"foo", "bar"}
  28. err := mock.RunSinkCollect(s, exp)
  29. if err != nil {
  30. t.Errorf(err.Error())
  31. return
  32. }
  33. result := getResults()
  34. if !reflect.DeepEqual(result, exp) {
  35. t.Errorf("result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", exp, result)
  36. }
  37. }
  38. func getResults() []string {
  39. f, err := os.Open(CACHE_FILE)
  40. if err != nil {
  41. panic(err)
  42. }
  43. result := make([]string, 0)
  44. scanner := bufio.NewScanner(f)
  45. for scanner.Scan() {
  46. result = append(result, scanner.Text())
  47. }
  48. if err := scanner.Err(); err != nil {
  49. panic(err)
  50. }
  51. _ = f.Close()
  52. return result
  53. }