69106605a48a7fcf4de9535ee954a37d44ae3d2d
[swifty.git] / src / libswift_udp / tests / connecttest.cpp
1 /*
2  *  connecttest.cpp
3  *  serp++
4  *
5  *  Created by Victor Grishchenko on 3/19/09.
6  *  Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
7  *
8  */
9 #include <sys/stat.h>
10 #include <time.h>
11 #include <gtest/gtest.h>
12 #include "swift.h"
13
14
15 using namespace swift;
16
17 struct event evcompl;
18 int size, copy;
19
20 void IsCompleteCallback(int fd, short event, void *arg) {
21     if (swift::SeqComplete(copy)!=size)
22         evtimer_add(&evcompl, tint2tv(TINT_SEC));
23     else
24         event_base_loopexit(Channel::evbase, NULL);
25 }
26
27 TEST(Connection,CwndTest) {
28
29     Channel::evbase = event_base_new();
30
31     srand ( time(NULL) );
32
33     unlink("test_file0-copy.dat");
34 #ifdef WIN32
35     struct _stat st;
36 #else
37     struct stat st;
38 #endif
39     ASSERT_EQ(0,stat("test_file0.dat", &st));
40     size = st.st_size;//, sizek = (st.st_size>>10) + (st.st_size%1024?1:0) ;
41     Channel::SELF_CONN_OK = true;
42
43     int sock1 = swift::Listen(7001);
44     ASSERT_TRUE(sock1>=0);
45
46     int file = swift::Open("test_file0.dat");
47     FileTransfer* fileobj = FileTransfer::file(file);
48     //FileTransfer::instance++;
49
50     swift::SetTracker(Address("127.0.0.1",7001));
51
52     copy = swift::Open("test_file0-copy.dat",fileobj->root_hash());
53
54     evtimer_assign(&evcompl, Channel::evbase, IsCompleteCallback, NULL);
55     evtimer_add(&evcompl, tint2tv(TINT_SEC));
56
57     //swift::Loop(TINT_SEC);
58     event_base_dispatch(Channel::evbase);
59
60     //int count = 0;
61     //while (swift::SeqComplete(copy)!=size && count++<600)
62     //    swift::Loop(TINT_SEC);
63     ASSERT_EQ(size,swift::SeqComplete(copy));
64
65     swift::Close(file);
66     swift::Close(copy);
67
68     swift::Shutdown(sock1);
69
70 }
71
72
73 int main (int argc, char** argv) {
74
75     swift::LibraryInit();
76     testing::InitGoogleTest(&argc, argv);
77     Channel::debug_file = stdout;
78     int ret = RUN_ALL_TESTS();
79     return ret;
80
81 }