github.com/Ryan-Johnson-1315/socketlogger@v0.0.2/csv_test.go (about)

     1  package socketlogger
     2  
     3  import (
     4  	"encoding/csv"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  var (
    13  	outputDir  string
    14  	incrememnt int
    15  )
    16  
    17  func init() {
    18  	t := &testing.T{}
    19  	outputDir = t.TempDir()
    20  	incrememnt = 0
    21  }
    22  
    23  func TestCsvCreation(t *testing.T) {
    24  	userver, uclient := newUdp()
    25  	tserver, tclient := newTcp()
    26  	defer func() {
    27  		uclient.Disconnect()
    28  		tclient.Disconnect()
    29  		time.Sleep(50 * time.Millisecond)
    30  		tserver.Shutdown()
    31  		userver.Shutdown()
    32  		time.Sleep(50 * time.Millisecond)
    33  	}()
    34  
    35  	uclient.NewCsvFile("udp_creation.csv", []interface{}{"hello", "from", "udp", "land", "udp_creation.csv"})
    36  
    37  	time.Sleep(100 * time.Millisecond) // Creating the file takes longer than sending the message
    38  	if !fileDirExists(outputDir, "udp_creation.csv") {
    39  		t.Errorf("%s log file was not created", filepath.Join(outputDir, "udp_creation.csv"))
    40  	}
    41  
    42  	tclient.NewCsvFile("tcp_creation.csv", []interface{}{"hello", "from", "udp", "land", "tcp_creation.csv"})
    43  	time.Sleep(100 * time.Millisecond) // Creating the file takes longer than sending the message
    44  	if !fileDirExists(outputDir, "tcp_creation.csv") {
    45  		t.Errorf("%s log file was not created", filepath.Join(outputDir, "tcp_creation.csv"))
    46  	}
    47  }
    48  
    49  func TestDisconnectCsv(t *testing.T) {
    50  	userver, uclient := newUdp()
    51  	tserver, tclient := newTcp()
    52  
    53  	rows := [][]interface{}{
    54  		{"Carina Haws", "2001-09-16", 3.1, "Washington University in Saint Louis, Missouri"},
    55  		{"Alice Gill", "2002-09-17", 3.9, "Rice University, Texas"},
    56  		{"Sharika Teeple", "2001-09-27", 2.9, "University of Maryland, College Park, Maryland"},
    57  		{"Earl Friel", "2002-12-03", 3.6, "University of Richmond, Virginia"},
    58  		{"Earlean Numbers", "2002-12-23", 3.3, "Mount Holyoke College, Massachusetts"},
    59  	}
    60  	ufname := "udp_disconnect.csv"
    61  	uclient.NewCsvFile(ufname, nil)
    62  	for _, row := range rows {
    63  		uclient.AppendRow(ufname, row)
    64  	}
    65  
    66  	uclient.Disconnect()
    67  	time.Sleep(500 * time.Millisecond) // Just enough to let the messages get to the server over the socket
    68  	userver.Shutdown()
    69  
    70  	file, terr := os.Open(filepath.Join(outputDir, ufname))
    71  	if terr != nil {
    72  		t.Errorf("Error opening file %v", terr)
    73  	}
    74  	ureader := csv.NewReader(file)
    75  	uoutputRows, uerr := ureader.ReadAll()
    76  	if uerr != nil {
    77  		t.Errorf("Error reading csv file %v", uerr)
    78  	}
    79  
    80  	if len(uoutputRows) == 0 {
    81  		t.Errorf("No rows written to file!")
    82  	}
    83  
    84  	for i, row := range uoutputRows {
    85  		for j, cell := range row {
    86  			if fmt.Sprint(rows[i][j]) != cell {
    87  				t.Errorf("Row %d, Col %d did not match. Actual: %s, Expected %s", i, j, fmt.Sprint(rows[i][j]), cell)
    88  			}
    89  		}
    90  	}
    91  
    92  	tfname := "udp_disconnect.csv"
    93  	tclient.NewCsvFile(tfname, nil)
    94  	for _, row := range rows {
    95  		tclient.AppendRow(tfname, row)
    96  	}
    97  
    98  	tclient.Disconnect()
    99  	time.Sleep(500 * time.Millisecond) // Just enough to let the messages get to the server over the socket
   100  	tserver.Shutdown()
   101  
   102  	tfile, terr := os.Open(filepath.Join(outputDir, tfname))
   103  	if terr != nil {
   104  		t.Errorf("Error opening file %v", terr)
   105  	}
   106  	treader := csv.NewReader(tfile)
   107  	toutputRows, terr := treader.ReadAll()
   108  	if terr != nil {
   109  		t.Errorf("Error reading csv file %v", terr)
   110  	}
   111  
   112  	if len(toutputRows) == 0 {
   113  		t.Errorf("No rows written to file!")
   114  	}
   115  
   116  	for i, row := range toutputRows {
   117  		for j, cell := range row {
   118  			if fmt.Sprint(rows[i][j]) != cell {
   119  				t.Errorf("Row %d, Col %d did not match. Actual: %s, Expected %s", i, j, fmt.Sprint(rows[i][j]), cell)
   120  			}
   121  		}
   122  	}
   123  }
   124  
   125  func TestCsvDuplicate(t *testing.T) {
   126  	userver, uclient := newUdp()
   127  	tserver, tclient := newTcp()
   128  	defer func() {
   129  		uclient.Disconnect()
   130  		tclient.Disconnect()
   131  		time.Sleep(50 * time.Millisecond)
   132  		tserver.Shutdown()
   133  		userver.Shutdown()
   134  	}()
   135  	udp := []struct {
   136  		input  string
   137  		output string
   138  	}{
   139  		{"udp_1.csv", "udp_1_1.csv"},
   140  		{"udp_2.csv", "udp_2_1.csv"},
   141  		{"udp_3.csv", "udp_3_1.csv"},
   142  		{"udp_4.csv", "udp_4_1.csv"},
   143  		{"udp_5.csv", "udp_5_1.csv"},
   144  		{"udp_6.csv", "udp_6_1.csv"},
   145  		{"udp_7.csv", "udp_7_1.csv"},
   146  	}
   147  
   148  	for _, test := range udp {
   149  		createFile(filepath.Join(outputDir, test.input))
   150  		uclient.AppendRow(test.input, []interface{}{"this", "is", "the", "duplicate", "headers"})
   151  		time.Sleep(100 * time.Millisecond)
   152  		if !fileDirExists(outputDir, test.output) {
   153  			t.Errorf("%s log file was not created", filepath.Join(outputDir, test.output))
   154  		}
   155  	}
   156  
   157  	tcp := []struct {
   158  		input  string
   159  		output string
   160  	}{
   161  		{"tcp_1.csv", "tcp_1_1.csv"},
   162  		{"tcp_2.csv", "tcp_2_1.csv"},
   163  		{"tcp_3.csv", "tcp_3_1.csv"},
   164  		{"tcp_4.csv", "tcp_4_1.csv"},
   165  		{"tcp_5.csv", "tcp_5_1.csv"},
   166  		{"tcp_6.csv", "tcp_6_1.csv"},
   167  		{"tcp_7.csv", "tcp_7_1.csv"},
   168  	}
   169  
   170  	for _, test := range tcp {
   171  		createFile(filepath.Join(outputDir, test.input))
   172  		tclient.AppendRow(test.input, []interface{}{"this", "is", "the", "duplicate", "headers"})
   173  		time.Sleep(100 * time.Millisecond)
   174  		if !fileDirExists(outputDir, test.output) {
   175  			t.Errorf("%s log file was not created", filepath.Join(outputDir, test.output))
   176  		}
   177  	}
   178  }
   179  
   180  func TestOutput(t *testing.T) {
   181  	userver, uclient := newUdp()
   182  	tserver, tclient := newTcp()
   183  	defer func() {
   184  		uclient.Disconnect()
   185  		tclient.Disconnect()
   186  		time.Sleep(50 * time.Millisecond)
   187  		tserver.Shutdown()
   188  		userver.Shutdown()
   189  	}()
   190  	rows := [][]interface{}{
   191  		{"one", "two", 3, 4.4, "yolo"},
   192  		{"one", "two", 3, 4.4, "yolo"},
   193  		{"one", "two", 3, 4.4, "yolo"},
   194  		{"one", "two", 3, 4.4, "yolo"},
   195  		{"one", "two", 3, 4.4, "yolo"},
   196  		{"one", "two", 3, 4.4, "yolo"},
   197  		{"one", "two", 3, 4.4, "yolo"},
   198  	}
   199  	udpFname := "udp_output_test.csv"
   200  	uclient.NewCsvFile(udpFname, nil)
   201  
   202  	for _, row := range rows {
   203  		uclient.AppendRow(udpFname, row)
   204  	}
   205  
   206  	time.Sleep(500 * time.Millisecond)
   207  
   208  	ufile, uerr := os.Open(filepath.Join(outputDir, udpFname))
   209  	if uerr != nil {
   210  		t.Errorf("Error opening file %v", uerr)
   211  	}
   212  	ureader := csv.NewReader(ufile)
   213  	outputRows, uerr := ureader.ReadAll()
   214  	if uerr != nil {
   215  		t.Errorf("Error reading csv file %v", uerr)
   216  	}
   217  
   218  	if len(outputRows) == 0 {
   219  		t.Errorf("No rows written to file!")
   220  	}
   221  
   222  	for i, row := range outputRows {
   223  		for j, cell := range row {
   224  			if fmt.Sprint(rows[i][j]) != cell {
   225  				t.Errorf("Row %d, Col %d did not match. Actual: %s, Expected %s", i, j, fmt.Sprint(rows[i][j]), cell)
   226  			}
   227  		}
   228  	}
   229  
   230  	tcpFname := "tcp_output_test.csv"
   231  	for _, row := range rows {
   232  		tclient.AppendRow(tcpFname, row)
   233  	}
   234  
   235  	time.Sleep(500 * time.Millisecond)
   236  
   237  	tfile, terr := os.Open(filepath.Join(outputDir, tcpFname))
   238  	if terr != nil {
   239  		t.Errorf("Error opening file %v", terr)
   240  	}
   241  	treader := csv.NewReader(tfile)
   242  	outputRows, err := treader.ReadAll()
   243  	if err != nil {
   244  		t.Errorf("Error reading csv file %v", err)
   245  	}
   246  
   247  	if len(outputRows) == 0 {
   248  		t.Errorf("No rows written to file!")
   249  	}
   250  
   251  	for i, row := range outputRows {
   252  		for j, cell := range row {
   253  			if fmt.Sprint(rows[i][j]) != cell {
   254  				t.Errorf("Row %d, Col %d did not match. Actual: %s, Expected %s", i, j, fmt.Sprint(rows[i][j]), cell)
   255  			}
   256  		}
   257  	}
   258  }
   259  
   260  func TestBadCsvFile(t *testing.T) {
   261  	userver, uclient := newUdp()
   262  	tserver, tclient := newTcp()
   263  	defer func() {
   264  		uclient.Disconnect()
   265  		tclient.Disconnect()
   266  		time.Sleep(50 * time.Millisecond)
   267  		tserver.Shutdown()
   268  		userver.Shutdown()
   269  	}()
   270  	userv := NewUdpCsvServer()
   271  	userv.SetOutputCsvDirectory("/dev")
   272  	userv.Bind(Connection{
   273  		Addr: "127.0.0.1",
   274  		Port: 43201,
   275  	})
   276  
   277  	uclient.AppendRow("should_fail.csv", nil)
   278  	time.Sleep(100 * time.Millisecond)
   279  	if fileDirExists("/dev", "should_fail.csv") {
   280  		t.Error("Created file when should not have created it")
   281  	}
   282  
   283  	tserv := NewTcpCsvServer()
   284  	tserv.SetOutputCsvDirectory("/dev")
   285  	tserv.Bind(Connection{
   286  		Addr: "127.0.0.1",
   287  		Port: 43202,
   288  	})
   289  
   290  	tclient.AppendRow("should_fail.csv", nil)
   291  	time.Sleep(100 * time.Millisecond)
   292  	if fileDirExists("/dev", "should_fail.csv") {
   293  		t.Error("Created file when should not have created it")
   294  	}
   295  }
   296  
   297  func createFile(path string) {
   298  	os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0o666)
   299  }
   300  
   301  func newUdp() (CsvServer, CsvClient) {
   302  	server := NewUdpCsvServer()
   303  	server.Bind(Connection{
   304  		Addr: "127.0.0.1",
   305  		Port: 42000 + incrememnt,
   306  	})
   307  	server.SetOutputCsvDirectory(outputDir)
   308  
   309  	client := NewUdpCsvClient()
   310  	client.Connect(Connection{
   311  		Addr: "127.0.0.1",
   312  		Port: 0,
   313  	}, Connection{
   314  		Addr: "127.0.0.1",
   315  		Port: 42000 + incrememnt,
   316  	})
   317  	incrememnt++
   318  	return server, client
   319  }
   320  
   321  func newTcp() (CsvServer, CsvClient) {
   322  	server := NewTcpCsvServer()
   323  	server.Bind(Connection{
   324  		Addr: "127.0.0.1",
   325  		Port: 43001 + incrememnt,
   326  	})
   327  	server.SetOutputCsvDirectory(outputDir)
   328  	client := NewTcpCsvClient()
   329  	client.Connect(Connection{
   330  		Addr: "127.0.0.1",
   331  		Port: 0,
   332  	}, Connection{
   333  		Addr: "127.0.0.1",
   334  		Port: 43001 + incrememnt,
   335  	})
   336  	incrememnt++
   337  	return server, client
   338  }