1/*
2** Zabbix
3** Copyright (C) 2001-2021 Zabbix SIA
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18**/
19
20package redis
21
22import (
23	"fmt"
24	"reflect"
25	"testing"
26
27	"github.com/mediocregopher/radix/v3"
28)
29
30func TestPlugin_pingHandler(t *testing.T) {
31	stubConn := radix.Stub("", "", func(args []string) interface{} {
32		return "PONG"
33	})
34	defer stubConn.Close()
35
36	conn := &RedisConn{
37		client: stubConn,
38	}
39
40	brokenStubConn := radix.Stub("", "", func(args []string) interface{} {
41		return ""
42	})
43	defer brokenStubConn.Close()
44
45	brokenConn := &RedisConn{
46		client: brokenStubConn,
47	}
48
49	closedStubConn := radix.Stub("", "", func(args []string) interface{} {
50		return ""
51	})
52	closedStubConn.Close()
53
54	closedConn := &RedisConn{
55		client: closedStubConn,
56	}
57
58	type args struct {
59		conn   redisClient
60		params map[string]string
61	}
62	tests := []struct {
63		name    string
64		args    args
65		want    interface{}
66		wantErr bool
67	}{
68		{
69			fmt.Sprintf("pingHandler should return %d if connection is ok", pingOk),
70			args{conn: conn},
71			pingOk,
72			false,
73		},
74		{
75			fmt.Sprintf("pingHandler should return %d if PING answers wrong", pingFailed),
76			args{conn: brokenConn},
77			pingFailed,
78			false,
79		},
80		{
81			fmt.Sprintf("pingHandler should return %d if connection failed", pingFailed),
82			args{conn: closedConn},
83			pingFailed,
84			false,
85		},
86	}
87	for _, tt := range tests {
88		t.Run(tt.name, func(t *testing.T) {
89			got, err := pingHandler(tt.args.conn, tt.args.params)
90			if (err != nil) != tt.wantErr {
91				t.Errorf("Plugin.pingHandler() error = %v, wantErr %v", err, tt.wantErr)
92				return
93			}
94			if !reflect.DeepEqual(got, tt.want) {
95				t.Errorf("Plugin.pingHandler() = %v, want %v", got, tt.want)
96			}
97		})
98	}
99}
100