libdispatch update
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_timer_set_time.c
index b15330d..f7821fc 100644 (file)
@@ -1,61 +1,80 @@
-#include "config/config.h"
+/*
+ * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
 
 #include <sys/time.h>
 #include <assert.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/time.h>
 
-#define        __DISPATCH_INDIRECT__
-#include "src/internal.h"
+#include <dispatch/dispatch.h>
 
+#include <bsdtests.h>
 #include "dispatch_test.h"
 
-int main(void)
+void
+test_timer(void)
 {
-       test_start("Dispatch Update Timer");
+       dispatch_test_start("Dispatch Update Timer");
 
        dispatch_queue_t main_q = dispatch_get_main_queue();
-       test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
+       //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
 
        __block int i = 0;
        struct timeval start_time;
 
        gettimeofday(&start_time, NULL);
-       dispatch_source_attr_t attr = dispatch_source_attr_create();
-       dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) {
+
+       dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
+       test_ptr_notnull("dispatch_source_create", s);
+
+       dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0);
+
+       dispatch_source_set_cancel_handler(s, ^{
                struct timeval end_time;
                gettimeofday(&end_time, NULL);
                // Make sure we actually managed to adjust the interval
                // duration.  Seven one second ticks would blow past
                // this.
                test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3);
-               test_ptr_notnull("finalizer ran", ds);
                test_stop();
+
+               dispatch_release(s);
        });
 
-       dispatch_source_t s = dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL,
-               1000000000ull,
-               0,
-               attr,
-               main_q,
-               ^(dispatch_event_t ev) {
-                       long err;
-                       if (dispatch_event_get_error(ev, &err)) {
-                               test_errno("dispatch_event_get_error", err, ECANCELED);
-                               dispatch_release(dispatch_event_get_source(ev));
-                       } else {
-                               fprintf(stderr, "%d\n", ++i);
-                               if (i >= 7) {
-                                       dispatch_cancel(dispatch_event_get_source(ev));
-                               } else if (i == 1) {
-                                       dispatch_source_timer_set_time(dispatch_event_get_source(ev), 100, 0);
-                               }
-                       }
-               });
+       dispatch_source_set_event_handler(s, ^{
+               fprintf(stderr, "%d\n", ++i);
+               if (i >= 7) {
+                       dispatch_source_cancel(s);
+               } else if (i == 1) {
+                       dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC / 10, 0);
+               }
+       });
        test_ptr_notnull("dispatch_source_timer_create", s);
 
-       dispatch_release(attr);
+       dispatch_resume(s);
+}
 
+int
+main(void) {
+       test_timer();
        dispatch_main();
 
        return 0;